如何实现django - pre_save和post_save?

时间:2014-03-07 07:49:50

标签: python django django-models django-signals

我已经尝试过很多来实现django的pre_save和post_save,但我仍然无法生成信号。
我所拥有的是:

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}

请告诉我我做错了什么?

1 个答案:

答案 0 :(得分:2)

类似的东西:

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)