← Back to Django Course | Chapter 4: Models & Django ORM Basics | Lesson 2 of 10

Model Fields को Define करना

एक field आपके model पर एक labeled box है, और यह decide करता है कि किस तरह का data — जैसे text, एक number, या एक date — Django आपको इसके अंदर डालने देगा।
Syntax
markup
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.

markup
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 जानना है।

Note: max_length database storage size के बारे में है, form validation length के बारे में नहीं।
Warning: एक CharField पर max_length omit करना migrate करने से पहले ही एक system check error raise करता है।

उदाहरण: 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.

markup
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 करते हैं।

Note: Text-based fields के लिए, null=True के बजाय एक empty string default के साथ blank=True prefer करें, 'no value' represent करने के दो तरीके avoid करने के लिए।

उदाहरण: 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.

markup
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.

markup
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. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक CharField पर max_length भूल जाना, जो makemigrations time पर एक error raise करता है क्योंकि CharField को इसकी ज़रूरत है।
  2. एक article body जैसे बहुत लंबे text के लिए TextField के बजाय CharField उपयोग करना, जिसकी कोई length limit नहीं है।
  3. एक 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 करने के लिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.