0

I'd like to send a notification to users when somebody makes a recommendation of the content they shared. I am using django-model-changes and the problem is similar to Django - send email on model change. So as in the mentioned case, I have...

class Recommendation(ChangesMixin, models.Model):
    recommend = models.BooleanField(default=None)

and then...

@receiver(post_save, sender=Recommendation)
def new_recommendation(sender, instance=None, created=False, **kwargs):

...

if created or instance.previous_instance().recommend is False:
    if instance.recommend is True:
    #send notification code

If the user now clicks "Recommend", the notification is sent. But the problem is, it is also sent if the first click is "Don't recommend". The value stored in the database is in that case False. But the notification is still sent.

If the user switches their recommendation later from True to False or the other way around, notifications are sent as they should be, only upon True. But the first False is the problem.

Does anyone understand why this is happening?

Edit:

To avoid the complication with previous_instance, I also tried the following...

if created and instance.recommend is True:
    #send notification code

The result is the same. If the first action is False (and False is definitely saved in the database), the code still gets executed.

Community
  • 1
  • 1
apiljic
  • 527
  • 4
  • 14

1 Answers1

0

You ask a Boolean Field (t/f) that is null if it is false. Since null evaluates to false it returns true. Better use a NullBooleanField.

Like a BooleanField, but allows NULL as one of the options. Use this instead of a BooleanField with null=True.

rollinger
  • 531
  • 1
  • 4
  • 15