Search code examples
pythondjangodjango-rest-frameworkdjango-signals

How can i make signals in django which can differentiate between put and post method?


I am using in_built Django user and want to generate notification for each added user but I don't want notification to generate on put request.It should only generate on post request . My model is...

Models.py

class User(AbstractUser):
    gender= models.CharField(choices=GENDER, max_length=10, null=True, blank=True)
    role= models.CharField(choices=ROLE_CHOICE, max_length=15, null=True, blank=True)
    designation=models.CharField(max_length=225,null=True,blank=True)
    employee_id = models.CharField(max_length=20, default=None, null=True)

class Notification(models.Model):
    text = models.CharField(max_length=225)
    recipient=models.ForeignKey(User,related_name='notification')
    timestamp = models.DateTimeField(null=True,blank=True,auto_now_add=True)
    unread = models.BooleanField(default=True, blank=False)
    send_by= models.ForeignKey(User, null=True,blank=True, related_name='Notification')

and in my signals I have made like....

Signals.py

@receiver(post_save, sender= Any_model)
def comment_recieved(sender,**kwargs):
    obj= kwargs.get('instance')
        recipient=User.objects.get(is_superuser=True)
        Notification.objects.create(
            recipient= recipient,
            comment= obj,
            project=obj.project,
            type="commented",
            send_by=obj.supporter,
            text= "%s has commented on %s" % (obj.supporter, obj.project)
        )
        return None

Now the problem is that everytime you do either a put or post request,it saves the user instance and so it creates a Notification object.I want to create Notification only when new user is created.


Solution

  • The post_save signal sends an argument called created which is

    A boolean; True if a new record was created.

    You can use this to differentiate between if a new record is created or just updated.

    More info in the docs