0

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??

MHS
  • 2,260
  • 11
  • 31
  • 45
  • You're referencing an uninitialized variable `obj` in the first line of the `save` method (the variable is initialized in the second line) – lanzz Mar 07 '14 at 08:38
  • 2
    You shouldn't be implementing any of that code. Both the sending of signals and the creation of the object are already done for you by the base model class. – Daniel Roseman Mar 07 '14 at 08:40

1 Answers1

2

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)
grigno
  • 3,128
  • 4
  • 35
  • 47