views:

29

answers:

1

Hi folks,

Sorry for some crazy subj.

I'd like to override django models save method and call some additional code if the model instance is newly created. Sure I can use signals or check if the model have empty pk field and if yes, create temporary variable and later call a code:

Class EmailModel(models.Model):
    email = models.EmailField()

    def save(self, *args, **kwargs)
        is_new = self.pk is None
        super(EmailModel, self).save(*args, **kwargs)
        # Create necessary objects
        if is_new:
            self.post_create()

    def post_create(self):
          # do job, send mails
          pass

But I like to have some beautiful code and avoid using temporary variable in save method. So the question is: is it possible to find if the instance of model is newly created object just after super save_base parent method call?

I've checked django sources can't find how to do that in right way.

Thanks

We have related post

+2  A: 

For real - signals are best approch in this case.

You could use post_save() signal and in the listener just check if the credit_set exist for current model instance and if not - create one. That would be my choice - there is no need to overdo such a simple task.

Of course if you really need to know exactly when the model was initiated (I doubt it) use post_init() signal. You don't need to override save() method just to set some additional variables. Just catch post_init() signal, or pre_save(), and just change/add what you want. IMHO there is no sense to override save() method and check if this is new instance or not - that's why the signals are there.

bx2
I won't agree with you. Signals are good when you pass data between different applications, but if the action have direct relation to the model it's better to override save. Here is good explanation when you should use both: http://www.martin-geber.com/thought/2007/10/29/django-signals-vs-custom-save-method/
HardQuestions
I don't need "post_init()". For example I'm just trying to send some mail if new instance of model is created.
HardQuestions
No sense in putting mailing logic into save() method then :) Back to the basics - that's why the signals are for. You override save() method if you want to ie. add some customer automatic number for accounting system, but not for sending emails. You could use `send_mail()` in catched `post_save()` signal - instance and sender are provided as arguments in this case :)
bx2
I agree, that's wrong example.
HardQuestions
Ok then - please explain what exactly you want to achieve? You want to send mail, or you want to create some related objects or maybe something different?
bx2
All is simple - avoid signals and avoid is_new additional variable. I'd like to know maybe the instance have some internal property that can tell me if it's newly created or not.
HardQuestions
Did you looked at this code? http://code.djangoproject.com/browser/django/trunk/django/db/models/base.py#L488, there is property pk_val definied here: http://code.djangoproject.com/browser/django/trunk/django/db/models/base.py#L396 - you could check this like in django base model code. Oh.. I've just noticed that you've already used pk in your code :(
bx2