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?
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)
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.
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.