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

सामान्य Model Field Types

Django आपको field types का एक toolbox देता है, और हर एक किसी specific kind of value, जैसे text, whole numbers, या dates, के लिए shaped है।
Syntax
markup
class ModelName(models.Model):
    text = models.CharField(max_length=100)
    long_text = models.TextField()
    number = models.IntegerField()
    flag = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

Text Fields

CharField एक maximum length वाले छोटे, single-line text के लिए है, जबकि TextField paragraphs जैसे लंबे text के लिए है, बिना किसी length restriction के।

उदाहरण: Text Fields

CharField is for short, single-line text with a maximum length, while TextField is for long text like paragraphs, with no length restriction.

markup
from django.db import models


class Article(models.Model):
    headline = models.CharField(max_length=120)
    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. #}

Numeric Fields

IntegerField whole numbers store करता है। Django के पास decimals के लिए FloatField भी है, लेकिन simple counters और quantities के लिए IntegerField usually काफी है।

उदाहरण: Numeric Fields

IntegerField stores whole numbers. Django also has FloatField for decimals, but for simple counters and quantities IntegerField is usually enough.

markup
from django.db import models


class Inventory(models.Model):
    item_name = models.CharField(max_length=100)
    quantity = 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. #}

Boolean Fields

BooleanField एक simple True या False value store करता है, flags जैसे कि एक task complete है या एक user active है, के लिए perfect।

उदाहरण: Boolean Fields

BooleanField stores a simple True or False value, perfect for flags like whether a task is complete or a user is active.

markup
from django.db import models


class Task(models.Model):
    title = models.CharField(max_length=150)
    is_complete = 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. #}

Date and Time Fields

DateTimeField दोनों एक date और एक time store करता है। auto_now_add=True इसे अपने आप एक बार stamp करता है, जब object पहली बार create हो।

Note: Creation timestamp के लिए auto_now_add और updated-at timestamp के लिए auto_now उपयोग करें — कभी एक ही field पर दोनों नहीं।

उदाहरण: Date and Time Fields

DateTimeField stores both a date and a time. auto_now_add=True stamps it automatically once, when the object is first created.

markup
from django.db import models


class LogEntry(models.Model):
    message = models.TextField()
    created_at = models.DateTimeField(auto_now_add=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. #}
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. कभी negative न होने वाली values (जैसे age) के लिए PositiveIntegerField के बजाय IntegerField उपयोग करना।
  2. Dates और times को CharField text के रूप में store करना DateTimeField के बजाय, date के हिसाब से properly sort या filter करने की ability खोते हुए।
  3. एक simple built-in field जैसे CharField या BooleanField पहले से काम करता है यह check करने से पहले exotic third-party field packages तक पहुंचना।
चैप्टर सारांश
  • CharField छोटा text store करता है और इसे max_length चाहिए; TextField बिना किसी limit के लंबा text store करता है।
  • IntegerField whole numbers store करता है; BooleanField True/False values store करता है।
  • DateTimeField एक combined date और time store करता है, अक्सर auto_now_add या auto_now के साथ paired।
  • सही field type चुनना validation और database storage को शुरू से ही correct रखता है।

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.