Tuesday, November 10, 2015

Django override save method

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()

1 comment:

  1. It’s the best time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I want to suggest you few interesting things or suggestions.You can write next articles referring to this article. I desire to read even more things about it..
    SAP HR Training in Chennai
    SAP ABAP Training in Chennai
    SAP FICO Training in Chennai

    ReplyDelete