Search code examples
pythondjangoformsmodelssuper

clarification/when/where to use super in django/python


Actually we know super is used to find the "parent class" and return its object, something like/using self.__class__.__mro__

What I was confused about is when to use it exactly?

Suppose we have a Example model code as below

version_1

class Example(models.Model):
    name = models.CharField()
    age  = models.IntegerField()

    def save(self, **args, **kwargs):
        obj = super(Example, self).save(self, **args, **kwargs)
        obj.name = "name changed"
        obj.age = "age changed"
        return obj

version_2

class Example(models.Model):
    name = models.CharField()
    age  = models.IntegerField()

    def save(self, **args, **kwargs):
        self.name = "name changed"
        self.age = "age changed"
        obj = super(Example, self).save(self, **args, **kwargs)
        return obj

so as we can observe above

In version_1 I have called super first and done modifications to fields and returned the obj

In version_2 I had modified the fields and after that called super and returned obj

So what happens when super is called before and after modification of fields?

Finally what i want to know/confirm is

  • Where/why exactly super is used in django forms/models.
  • What is the exact concept of using them in django / python (if I understood this wrong).

Solution

  • Your Example class is a subclass of Model. That means, that Example inherits all functions from Model.

    Look at the following:

    class Example():
        pass
    
    example = Example()
    #let's try to save it
    example.save()
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: Example instance has no attribute 'save'
    

    Now with inheritance.

    class Parent():
        def save(*args, **kwargs):
            print "saving now"
    
    class Example(Parent):
        pass
    
    example = Example()
    #since Example has no save method, it inherits the Parent's save method.
    example.save()
    "saving now"
    

    If you override the save() method in your Example class, the Parent's save method will not be called.

       class Example(Parent):
            def save(self, *args, **kwargs):
                print "i know how to save on my own now"
    
       example = Example()
       example.save()
       "i know how to save on my own now"
    

    If you choose to call super, you are calling the Parent's save function together with your own implementation of save().

       class Example(Parent):
             def save(self, *args, **kwargs):
                  super(Example, self).save(*args, **kwargs)
                  print "i know how to save on my own now"
    
       example = Example()
       example.save()
       "saving now" #from parent
       "i know how to save on my own" #from example
    

    The same applies to all of djangos classes you inherit from. In fact, the implementation is more complex. You can take a look at the Model definition here on github.

    If you feel thrilled, you can dive into the django ORM with this talk