简体   繁体   中英

How to Implement django - pre_save and post_save?

I have tried much to implement django's pre_save and post_save, but still I am unable to generate the signal.
What I have is:

Class Client(models.Model):
    .
    .
    . # some fields

Class ClientView(models.Model):
    .
    .
    . # some fields
    class Meta:
        managed = False
        db_table = u'clients_view'
        verbose_name = 'Client'
        verbose_name_plural = 'Clients'

    def save(self):
        models.signals.pre_save.send(sender=obj, instance=self)
        obj = Client(**self.obj_to_dict())
        obj.save()
        models.signals.post_save.send(sender=obj, instance=self, created=True)

    def obj_to_dict(self):
        return {'pk': self.pk, 'name': self.name,
                'i_company': self.i_company, 'is_reseller': False}

Please Tell me where I am doing it wrong??

something like:

Class ClientView(models.Model):
#...your model definition...

def your_def(sender, instance, created, **kwargs):
        if created:
            client_view = instance
            #.....

post_save.connect(your_def, sender=ClientView)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM