0Pricing
Django Academy · درس

تعريف فئة Model

حوّل فئة Python إلى جدول قاعدة بيانات

تعريف فئة Model درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

A Class That Becomes a Table

In Django, a model is a Python class that describes one kind of data. Each model becomes a real database table for you.

Inherit from models.Model

Every model subclasses models.Model. That parent gives your class all the powers to talk to the database. 🗄️

from django.db import models

class Book(models.Model):
    pass

Fields Are Class Attributes

You describe columns by adding fields as class attributes. Each field is one column in the table Django will build.

class Book(models.Model):
    title = models.CharField(max_length=200)

One Field, One Column

Think of it simply: one field maps to one column. Add a field and you add a column to the table.

class Book(models.Model):
    title = models.CharField(max_length=200)
    pages = models.IntegerField()

Models Live in models.py

You write your models inside each app's models.py file. Django knows to look there for the data shapes of that app.

The Automatic Primary Key

Django quietly adds an id field as the primary key. You get a unique identifier for every row without writing any code.

No SQL Required

You never write CREATE TABLE by hand. The ORM turns your Python class into SQL, so you stay in Python the whole time.

Class Name to Table Name

By default the table name is the app label plus the lowercased class name, like app_book. Django picks a sensible name for you.

Instances Are Rows

Each instance of your model is one row of data. Create a Book object and you are describing one book record.

book = Book(title="Dune", pages=412)

Singular, CapitalizedNames

Name models in singular CamelCase: use Book, not Books. Django adds plurals where it needs them in the admin.

Migrations Make It Real

A model is just a plan until you run migrations. That step creates the matching table inside your real database.

Quick Check

Let's confirm what a Django model actually maps to.

Recap: Models in a Nutshell

You learned that a model is a class extending models.Model, its fields become columns, and migrations turn it into a real table. Nice start! 🎉

الأسئلة الشائعة

هل درس «تعريف فئة Model» مجاني؟

نعم — نص درس «تعريف فئة Model» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «تعريف فئة Model»؟

حوّل فئة Python إلى جدول قاعدة بيانات تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟

لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «تعريف فئة Model»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟

نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تعريف فئة Model
  2. اختيار أنواع الحقول
  3. خيارات الحقل: null وblank وdefault
  4. ‏str وفئة Meta
← العودة إلى Django Academy