Post create instance code call in django models -


sorry crazy subj.

i'd override django models save method , call additional code if model instance newly created. sure can use signals or check if model have empty pk field , if yes, create temporary variable , later call code:

class emailmodel(models.model):     email = models.emailfield()      def save(self, *args, **kwargs)         is_new = self.pk none         super(emailmodel, self).save(*args, **kwargs)         # create necessary objects         if is_new:             self.post_create()      def post_create(self):           # job, send mails           pass 

but have beautiful code , avoid using temporary variable in save method. question is: possible find if instance of model newly created object after super save_base parent method call?

i've checked django sources can't find how in right way.

thanks

we have related post

for real - signals best approch in case.

you use post_save() signal , in listener check if credit_set exist current model instance , if not - create one. that choice - there no need overdo such simple task.

of course if really need know when model initiated (i doubt it) use post_init() signal. don't need override save() method set additional variables. catch post_init() signal, or pre_save(), , change/add want. imho there no sense override save() method , check if new instance or not - that's why signals there.


Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -