[Answered ]-Django pre_save signals work when Model is saved but not ModelForm?

2๐Ÿ‘

โœ…

Iโ€™m not sure why it does not work, but I would suggest you to move that function to your models save method.

class ClientContact(models.Model):
    title = models.CharField(_("Title"), max_length=50, blank=True, null=True)
    first_name = models.CharField(_("First name"), max_length=30, )
    last_name = models.CharField(_("Last name"), max_length=30, )
    email = models.EmailField(_("Email"), blank=True, max_length=75)
    create_date = models.DateField(_("Creation date"))
    address = models.ForeignKey('AddressBook', blank=True, null=True, on_delete=models.SET_NULL)
    pipeline = models.ForeignKey(Pipeline, blank=True, null=True, on_delete=models.SET_NULL)

    def save(self, *args, **kwargs):
        #check if obj is new or being updated
        try:
            obj = ClientContact.objects.get(pk=self.pk)
            if not obj.pipeline == self.pipeline:  # Field has changed
                self.pipeline_updated()
        except ObjectDoesNotExist:
            pass
        #call super and store data
        super(ClientContact, self).save(*args, **kwargs)
๐Ÿ‘คrenno

Leave a comment