Model Fields को Define करना
class ModelName(models.Model):
field_name = models.CharField(max_length=100)
other_field = models.IntegerField(default=0, null=False, blank=False)
एक Field Declare करना
एक field एक class attribute के रूप में declared है जो एक field class instance पर set होता है, जैसे models.CharField(...). Django इसे matching database column बनाने के लिए उपयोग करता है।
उदाहरण: Declaring a Field
A field is declared as a class attribute set to a field class instance, such as models.CharField(...). Django uses this to build the matching database column.
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.IntegerField()
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Required Field Arguments
कुछ fields को correctly काम करने के लिए arguments चाहिए। CharField को हमेशा max_length चाहिए, क्योंकि Django को database level पर column का maximum size जानना है।
उदाहरण: Required Field Arguments
Some fields require arguments to work correctly. CharField always needs max_length, since Django needs to know the column's maximum size at the database level.
from django.db import models
class Comment(models.Model):
author_name = models.CharField(max_length=80)
body = models.TextField()
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Optional Field Behavior
blank=True control करता है कि एक field forms में empty छोड़ा जा सकता है या नहीं, जबकि null=True control करता है कि database column खुद NULL store कर सकता है या नहीं। वे अलग problems solve करते हैं।
उदाहरण: Optional Field Behavior
blank=True controls whether a field can be left empty in forms, while null=True controls whether the database column itself can store NULL. They solve different problems.
from django.db import models
class Profile(models.Model):
bio = models.TextField(blank=True, default='')
nickname = models.CharField(max_length=50, blank=True, null=True)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Default Values
default argument एक field को एक starting value देता है जब भी एक नया object उस field को explicitly set किए बिना बनाया जाए।
उदाहरण: Default Values
The default argument gives a field a starting value whenever a new object is created without explicitly setting that field.
from django.db import models
class Order(models.Model):
quantity = models.IntegerField(default=1)
is_paid = models.BooleanField(default=False)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- एक CharField पर max_length भूल जाना, जो makemigrations time पर एक error raise करता है क्योंकि CharField को इसकी ज़रूरत है।
- एक article body जैसे बहुत लंबे text के लिए TextField के बजाय CharField उपयोग करना, जिसकी कोई length limit नहीं है।
- एक field को बिना default या null=True के छोड़ना, फिर पहले से rows वाली एक table migrate करते समय एक error मिलना।
- Fields एक model पर class attributes हैं, हर एक CharField या IntegerField जैसी एक field class का instance।
- हर field type database में एक specific column type और validation rule से map करता है।
- CharField को max_length चाहिए; TextField या IntegerField जैसे दूसरे fields को नहीं चाहिए।
- Fields default, blank, और null जैसे options ले सकते हैं इनका behavior control करने के लिए।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: