In the django code in order to make a model,
for example,
class Student(models.Model):
name = models.CharField(max_length = 200)
Why models.CharField(max_length = 200). Why can’t we write only name=CharField(max_length = 200)
>Solution :
You can. You should only import that field in the scope, for example with:
from django.db import models
from django.db.models import CharField # 🖘 import CharField
class Student(models.Model):
name = CharField(max_length=200) # 🖘 use CharField
The field classes like CharField, IntegerField, … are all members of the django.db.models module, so you can import these in the head of the file.
You can also import Model and thus work with:
from django.db import models
from django.db.models import CharField, Model # 🖘 import Model
class Student(Model): # 🖘 use Model
name = CharField(max_length=200)