सामान्य Model Field Types
In this page:
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.
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.
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.
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 हो।
उदाहरण: 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.
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. #}
- कभी negative न होने वाली values (जैसे age) के लिए PositiveIntegerField के बजाय IntegerField उपयोग करना।
- Dates और times को CharField text के रूप में store करना DateTimeField के बजाय, date के हिसाब से properly sort या filter करने की ability खोते हुए।
- एक 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 रखता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: