Django Models का परिचय
from django.db import models
class ModelName(models.Model):
field_name = models.FieldType(options)
def __str__(self):
return self.field_name
एक Model क्या है
एक Django model एक Python class है जो models.Model से inherit करता है। हर attribute जो आप इस पर define करते हैं Django इसके लिए बनाई database table में एक column बन जाता है।
उदाहरण: What a Model Is
A Django model is a Python class that inherits from models.Model. Each attribute you define on it becomes a column in the database table Django creates for it.
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
pages = models.IntegerField()
def __str__(self):
return self.title
{# 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. #}
Models कहां रहते हैं
हर Django app की अपनी models.py file होती है। Django हर INSTALLED_APPS app में हर model पढ़ता है यह पता लगाने के लिए कि पूरे project को किन tables की ज़रूरत है।
उदाहरण: Where Models Live
Every Django app has its own models.py file. Django reads every model in every INSTALLED_APPS app to figure out what tables the whole project needs.
# myapp/models.py
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
grade = 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. #}
Model से Table तक
एक model class define करना अपने आप database table नहीं बनाता। Django को Python class को असली SQL में translate करने के लिए जो table बनाता है, एक migration चाहिए।
उदाहरण: From Model to Table
Defining a model class doesn't create the database table by itself. Django needs a migration to translate the Python class into real SQL that builds the table.
python manage.py makemigrations
python manage.py migrate
⚠️ Run this in your own terminal or Node.js environment.
एक Complete Minimal Model
एक working model को minimum एक class name, एक inherited base, और कम से कम एक field चाहिए। Django आपके लिए अपने आप एक auto-incrementing primary key id add करता है।
उदाहरण: A Complete Minimal Model
A working model needs at minimum a class name, an inherited base, and at least one field. Django automatically adds an id auto-incrementing primary key for you.
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=150)
is_done = models.BooleanField(default=False)
def __str__(self):
return self.title
{# 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. #}
- एक model बनाने के बाद makemigrations और migrate चलाना भूल जाना, इसलिए database table असल में कभी नहीं बनता।
- एक model define करना लेकिन इसे कहीं कभी register न करना, फिर यह सोचना कि Django admin या queries इसे क्यों नहीं देखते।
- Model class को lowercase (book) में नाम देना CamelCase (Book) के बजाय, Django की naming conventions तोड़ते हुए।
- एक Django model एक Python class है जो models.Model से inherit करता है और एक database table से map करता है।
- हर class attribute जो एक field के रूप में defined है उस table में एक column बन जाता है।
- Django ORM के through rows create, read, update, और delete करने के लिए ज़रूरी SQL अपने आप generate करता है।
- Models एक app की models.py file में रहते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: