-1

In the case creating a model, for example class Student(models.Model)
name=models.charfield(),roll=models.integerfield()

similarly, In the case creating a form, class newform(forms.Form) name=forms.charfield(),roll=forms.integerfield()

similarly, In the case creating a serializer, class serial(serializers.Serializer) name=serializers.charfield(),roll=serializers.integerfield()

I understood that in each classes,a base class is inherited but i am confused that if different objects of different classes are created inside a class in each scenario then what is the meaning of inheriting models.model, forms.Form,serializers.Serializer what these inherited classes do?

1
  • You inherit their methods so you don't have to re-type them. Do you want an example? Commented Jan 15, 2022 at 11:46

2 Answers 2

1

Django uses inheritance as well as object composition which are techniques of OOP for reusability.

Let us take your first class as example (I have only kept one field for simplicity):

Student(models.Model):
   name = models.CharField(max_length=100)

Inheritance:

The first line Student(model.Model): does inheritance by inheriting from Model class using which you are getting methods like save(), delete(), clean_fields e.t.c. Now your Student class can reuse those methods.

Composition

The second line name = models.CharField(max_length=100) does object composition by creating object namely name of class CharField using which you get methods like check, get_internal_type e.t.c.

All of those Inbuilt classes (Model, CharField e.t.c) are defined in file namely models.py so when you do models.Model you are getting Model class from file models.py and models.CharField gives you CharField class from same file.

Sign up to request clarification or add additional context in comments.

Comments

1

By inheriting from other classes, you have access to their methods;

Class A(object): 
    def _print(self): 
        print('Class A') 

Class B(A): 
    def other_print(self): 
        print('Class B')

if __name__ == "__main__":
    a, b = A(), B() 

    a._print()

    b._print()
    b.other_print()

When inheriting from model, forms, etc... You inherit from an object that is already integrated in the framework and thus has specific methods to work with the framework. For example the model will be registered to the database, the form 'knows' how to render properly, etc...

When you inherit from these classes, you already have an pre-built object with all these methods.

2 Comments

so these inherited classes do have some methods which are initiated automatically, right?
Yes, you are correct. In python when you need to be sure of the order of instantiation (for multiple inheritance) use super(ClassToInstanciate, self).__init__(*args) otherwise it should be handled automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.