'm trying to send a post_save signal after saving an instance through DRF 
ModelViewSet create api.
To send the signal only when a particular field is changed, I'm comparing 
its value with its original value inside signal as suggested here 
<https://stackoverflow.com/questions/1355150/django-when-saving-how-can-you-check-if-a-field-has-changed>
 like 
below.

if  instance._Modelname__original_fieldname != instance.fieldname:
    send a mail.

While when saving the model from Django admin, the above code is able to 
detect the change in values of the given field, the same code is showing 
same values for instance.fieldname and 
instance._Modelname__original_fieldname. Hence the mail is not getting sent 
when saving the model through api.

I've overridden the init and save methods in my model to enable the change 
detection of a field as below.

    def __init__(self, *args, **kwargs):
        super(Model, self).__init__(*args, **kwargs)
        self.__original_fieldname = self.fieldname

    def save(self, force_insert=False, force_update=False, *args, **kwargs):
        super(Model, self).save(force_insert, force_update, *args, **kwargs)
        self.__original_fieldname = self.fieldname

Why DRF is behaving differently for the same piece of code which django is 
running smoothly.
and If not as above, then how can I detect change in a field and send a 
signal, after DRF api save.

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to