django-signals

Facebook like notification updates using django signal or notification

霸气de小男生 提交于 2019-11-28 15:42:20
问题 How can i use django-notifications or django-signals to make something like facebook updates notification that shows in the user profile if any other user likes or posts comments on user's blog or posts? 回答1: For the activity feed, we use https://github.com/justquick/django-activity-stream Documentation: http://justquick.github.com/django-activity-stream/ For the js widget and live notifications, we use https://github.com/subsume/django-subscription yourlabs example, it depends on redis but

Django: UserProfile with Unique Foreign Key in Django Admin

白昼怎懂夜的黑 提交于 2019-11-28 08:36:48
I have extended Django's User Model using a custom user profile called UserExtension . It is related to User through a unique ForeignKey Relationship, which enables me to edit it in the admin in an inline form! I'm using a signal to create a new profile for every new user: def create_user_profile(sender, instance, created, **kwargs): if created: try: profile, created = UserExtension.objects.get_or_create(user=instance) except: pass post_save.connect(create_user_profile, sender=User) (as described here for example: Extending the User model with custom fields in Django ) The problem is, that, if

Save the related objects before the actual object being edited on django admin

浪尽此生 提交于 2019-11-28 08:31:31
Is it possible to save the related objects before the actual object being edited on a django admin form? For example: in models.py class Parent(model.Model): pass class Child(model.Model): parent = models.ForeignKey(Parent) @receiver(post_save,sender = Parent) def notify_parent_save(sender, instance=None, **kwargs): print "Parent save" @receiver(post_save,sender = Child) def notify_child_save(sender, instance=None, **kwargs): print "Child saved" in admin.py class ChildInline(admin.TabularInline): model = Child extra = 1 class ParentsAdmin(admin.ModelAdmin): inlines = [ChildInline] admin.site

Django manytomany signals? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 07:51:51
This question already has an answer here: django manytomanyfield .add() method 2 answers Let's say I have such model class Event(models.Model) users_count = models.IntegerField(default=0) users = models.ManyToManyField(User) How would you recommend to update users_count value if Event add/delete some users ? If possible in your case, you could introduce Participation model which would join Event and User: class Participation(models.Model): user = models.ForeignKey(User) event = models.ForeignKey(Event) class Event(models.Model): users = models.ManyToManyField(User, through='Participation') And

Why does Django post_save signal give me pre_save data?

你离开我真会死。 提交于 2019-11-28 07:22:31
问题 Im trying to connect a "Information" object to many "Customers" (see code below) When one Information object is updated, I want to send email to each Customer that is connected to the Information. However, when I log the sold_to field that the signal recieves I always get what the data is like BEFORE the save. I'm guessing this is because its ManyToManyField and the data is stored in a separate table, but shouldn't the post_save signal be called after all relations have been updated? Anyone

Disconnect signals for models and reconnect in django

巧了我就是萌 提交于 2019-11-28 07:20:48
I need make a save with a model but i need disconnect some receivers of the signals before save it. I mean, I have a model: class MyModel(models.Model): ... def pre_save_model(sender, instance, **kwargs): ... pre_save.connect(pre_save_model, sender=MyModel) and in another place in the code i need something like: a = MyModel() ... disconnect_signals_for_model(a) a.save() ... reconnect_signals_for_model(a) Because i need in this case, save the model without execute the function pre_save_model. For a clean and reusable solution, you can use a context manager: class temp_disconnect_signal(): """

Identify the changed fields in django post_save signal

喜你入骨 提交于 2019-11-28 07:12:53
I'm using django's post_save signal to execute some statements after saving the model. class Mode(models.Model): name = models.CharField(max_length=5) mode = models.BooleanField() from django.db.models.signals import post_save from django.dispatch import receiver @receiver(post_save, sender=Mode) def post_save(sender, instance, created, **kwargs): # do some stuff pass Now I want to execute a statement based on whether the value of the mode field has changed or not. @receiver(post_save, sender=Mode) def post_save(sender, instance, created, **kwargs): # if value of `mode` has changed: # then do

django post_save call from within sending Model?

巧了我就是萌 提交于 2019-11-28 04:56:59
问题 I have a pretty simple model that works: class Badge(models.Model): name = models.CharField(max_length=16, help_text="Name for Badge") category = models.ForeignKey(BadgeCategory, help_text="Category for badge") description = models.CharField(max_length=32, help_text="A brief description") file = models.ImageField(upload_to=format_badge_name) signals.post_save.connect(create_badge, sender=Badge) I know my create_badge function in signals.py works. If I send it without a value for sender, it

How can I have a Django signal call a model method?

给你一囗甜甜゛ 提交于 2019-11-28 02:49:28
问题 Maybe it's just late, but I cannot figure out why this isn't working. When I have a post_save signal call a generic function, it works, but when I have a post_save signal call a method from a model, nothing happens. Here is code that works: class Revision(models.Model): # Model junk... def send_email(sender, instance, created, **kwargs): if created: print "DO STUFF" signals.post_save.connect(send_email, sender=Revision) But this does not work: class Revision(models.Model): # Model junk... def

TransactionManagementError “You can't execute queries until the end of the 'atomic' block” while using signals, but only during Unit Testing

北战南征 提交于 2019-11-27 17:08:39
I am getting TransactionManagementError when trying to save a Django User model instance and in its post_save signal, I'm saving some models that have the user as the foreign key. The context and error is pretty similar to this question django TransactionManagementError when using signals However, in this case, the error occurs only while unit testing . It works well in manual testing, but unit tests fails. Is there anything that I'm missing? Here are the code snippets: views.py @csrf_exempt def mobileRegister(request): if request.method == 'GET': response = {"error": "GET request not accepted