Custom Search

Sunday, December 5, 2010

Django override save method (two ways)

Django override save method (two ways)

1)
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()

def save(self, *args, **kwargs):
do_something()
# Call the "real" save() method in the base class 'models.Model'
super(Blog, self).save(*args, **kwargs) <-----
do_something_else()

is equal to

2)
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()

def save(self, *args, **kwargs):
do_something()
# Call the "real" save() method in the base class 'models.Model'.
models.Model.save(self, *args, **kwargs) <----- Note: self
do_something_else()

2 comments: