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

Django Models का परिचय

एक model एक form template जैसा है जो Django को exactly बताता है कि database table में एक kind of thing के लिए कौन से boxes (fields) draw करने हैं, जैसे एक Book या एक Student।
Syntax
markup
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 बन जाता है।

Note: Model class names singular और CamelCase होते हैं, जैसे Book, न कि Books या book।

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

markup
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 की ज़रूरत है।

Warning: Model define करने वाले app को INSTALLED_APPS में list किया जाना चाहिए या Django इसे detect नहीं करेगा।

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

markup
# 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 चाहिए।

Note: एक model add या change करने के ठीक बाद हमेशा makemigrations फिर migrate चलाएं।

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

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

markup
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. #}
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. एक model बनाने के बाद makemigrations और migrate चलाना भूल जाना, इसलिए database table असल में कभी नहीं बनता।
  2. एक model define करना लेकिन इसे कहीं कभी register न करना, फिर यह सोचना कि Django admin या queries इसे क्यों नहीं देखते।
  3. 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 में रहते हैं।

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.