Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In case of Django model,

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading