views:

99

answers:

2

Hi guys, I would like to learn how to work with the Django Admin. How do I know when the user is editing an existing object or saving a new object?

For example, if I want to make a function to do something different when the user saves a new object or saves an edited object, how do I know which is which?

Thanks guys :)

Sorry for my English.

+5  A: 
class MyModel(models.Model):

    def save(self):
        if self.id != None:
            print "Edited object"
        else:
            print "New object"
        super(MyModel, self).save()
Adam
The id attribute of the model would be None if you are saving it for the first time.
Yuvi
Thanks a lot guys
Asinox
+1  A: 

You probably could do one of the following:

  1. Listening for Django signals. In your case the pre_save signal could be useful.
  2. Overwriting ModelAdmin.save_model
  3. Overwriting your models save method like described by Adam