django-signals

Django Signals to update a different model

微笑、不失礼 提交于 2019-12-02 13:20:02
问题 Say I have two models: class Product(models.Model): product = model.CharField() quantity = model.IntegerField() class sale(models.Model) product = models.ManyToManyField(Product) number_sold = model. IntegerField() When a sale is made, I'd like the product quantity to be updated. Are signals the best way of going about this? I was thinking about having sale admit a post_save signal when an object is created or updated. If updated, I would have to know the old and the new values to adjust the

How can I send signals from within Django migrations?

ε祈祈猫儿з 提交于 2019-12-01 15:32:10
I use Django 1.7 migrations, and in particular, want to populate a newly-created database with initial data. Thus, I use a data migration for this. It looks like this: def populate_with_initial_data(apps, schema_editor): User = apps.get_model("auth", "User") new_user = User.objects.create(username="nobody") class Migration(migrations.Migration): ... operations = [ migrations.RunPython(populate_with_initial_data), ] At the same time, I want to have an instance of the UserDetails model for every new user: @receiver(signals.post_save, sender=django.contrib.auth.models.User) def add_user_details

Cant get post_save to work in Django

房东的猫 提交于 2019-12-01 12:02:34
I read the django docs about signals and wrote this piece of code for my model Car : @receiver(request_finished) def signal_callback(sender, **kwargs): print 'Save Signal received' @receiver(post_save, sender=Car) def signal_handler(sender, **kwargs): pass request_finished(signal_callback, sender=car, dispatch_url="Unique save id") But the problem is, that when I fire up my server, and just open up the admin, I get a lot of 'Save Signal received' in my terminal. What I am wondering about is I have restricted the signal_handler to post_save only. But still, without even saving anything, the

Cant get post_save to work in Django

送分小仙女□ 提交于 2019-12-01 08:13:26
问题 I read the django docs about signals and wrote this piece of code for my model Car : @receiver(request_finished) def signal_callback(sender, **kwargs): print 'Save Signal received' @receiver(post_save, sender=Car) def signal_handler(sender, **kwargs): pass request_finished(signal_callback, sender=car, dispatch_url="Unique save id") But the problem is, that when I fire up my server, and just open up the admin, I get a lot of 'Save Signal received' in my terminal. What I am wondering about is I

Django Many-to-Many relation insertion control

£可爱£侵袭症+ 提交于 2019-12-01 05:18:45
I have the following models: class Item(models.Model): # fields # ... class Collection(models.Model): items = models.ManyToManyField(Item, related_name="collections") # other fields # ... Now I want two things: I want to control if an Item can be added to a Collection . I want the Collection to update some of its fields if an Item was added or removed. For the second issue I know that there is the django.db.models.signals.m2m_changed which I can use to hook into changes of the relation. Is it allowed/ok to change the Collection within the signal callback? Can I use the signal also for

Django Many-to-Many relation insertion control

老子叫甜甜 提交于 2019-12-01 02:22:35
问题 I have the following models: class Item(models.Model): # fields # ... class Collection(models.Model): items = models.ManyToManyField(Item, related_name="collections") # other fields # ... Now I want two things: I want to control if an Item can be added to a Collection . I want the Collection to update some of its fields if an Item was added or removed. For the second issue I know that there is the django.db.models.signals.m2m_changed which I can use to hook into changes of the relation. Is it

Trigering post_save signal only after transaction has completed

点点圈 提交于 2019-11-30 14:58:47
问题 I have written some APIs, for which the respective functions executive inside a transaction block. I am calling the save() method (after some modifications) on instance/s of a/several Model/s, and also consecutively indexing some JSON related information of the instance/s in Elasticsearch . I want the database to rollback even if for some reason the save() for one of the instances or indexing to the Elasticsearch fails. Now, the problem is arising that even inside the transaction block, the

Redirect User to another url with django-allauth log in signal

隐身守侯 提交于 2019-11-30 13:40:01
问题 I am using Django-allauth for my login/signup related stuff, so when a user signs up(first time) into my site, I am redirecting him to /thanks/ page by defining below setting in settings.py file LOGIN_REDIRECT_URL = '/thanks/' But when the user tried to log in for the next time(if already registered) I should redirect him to '/dashboard/' URL So tried to alter that with Django-allauth signals like below which is not working at all @receiver(allauth.account.signals.user_logged_in) def

Creating a profile model with both an InlineAdmin and a post_save signal in Django

[亡魂溺海] 提交于 2019-11-30 12:26:01
I created a 'profile' model (with a 1-to-1 relationship to the User model) as described on Extending the existing user model . The profile model has an optional many-to-one relationship to another model: class Profile(models.Model): user = models.OneToOneField(User, primary_key=True) account = models.ForeignKey(Account, blank=True, null=True, on_delete=models.SET_NULL) As documented there, I also created an inline admin: class ProfileInline(admin.StackedInline): model = Profile can_delete = False verbose_name_plural = 'profiles' # UserAdmin and unregister()/register() calls omitted, they are

Redirect User to another url with django-allauth log in signal

此生再无相见时 提交于 2019-11-30 10:56:58
I am using Django-allauth for my login/signup related stuff, so when a user signs up(first time) into my site, I am redirecting him to /thanks/ page by defining below setting in settings.py file LOGIN_REDIRECT_URL = '/thanks/' But when the user tried to log in for the next time(if already registered) I should redirect him to '/dashboard/' URL So tried to alter that with Django-allauth signals like below which is not working at all @receiver(allauth.account.signals.user_logged_in) def registered_user_login(sender, **kwargs): instance = User.objects.get_by_natural_key(kwargs['user']) print