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

285 views Asked by At

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.

2

There are 2 answers

0
Sagar Ramachandrappa On BEST ANSWER

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

0
Ivan Semochkin On

Suppose you use serializer for DRF view so you could override create() method of your User serializer.

class UserSerializer(serializer.ModelSerializer):
    ...
    def create(self, validated_data):
        user = User.objects.create(**validated_data)
        Notification.objects.create( # fill your fields here )
        return user

But more clear solution is writing custom manager and override create method there which will accept both fields for User and for Notification models:

class UserManager(models.Manager):
    ...
    def create(self, gender, role ... type #and so on):
        user = User(
            gender=gender
            role=role
            ...
            # other fields               
            )
        user.save()
        notification = Notification(
                           type=type 
                           ..  
                           # other fields
                           )      
        notification.save()         
        return user

class User(models.Model):
    ....
    objects = UserManager()

serializers:

    class UserSerializer(serializer.ModelSerializer):
        ...
        def create(self, validated_data):
            return User.objects.create(
                   gender=validated_data.get('gender')
                   role=validated_data.get('role')
                   ....
                   type = validated_data.get('type')