0Pricing
Flask Academy · درس

تعريف أول فئة نموذج لك

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

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

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

A Model Is a Table

In the ORM world, one Python class equals one database table. Each model describes the shape of the rows it will store. 🧱

Inherit from db.Model

Your class subclasses db.Model. That single base class gives it table powers like querying and saving for free.

class User(db.Model):
    pass

Class Name to Table Name

By default the table name is the lowercase class name. So a class named User maps to a table called user automatically.

Columns Are Class Attributes

Each field is a class attribute set to db.Column. The column type, like Integer or String, tells the database what to store.

name = db.Column(db.String(80))

Every Table Needs a Key

One column should be the primary key: a unique id for each row. It is how the database tells your records apart.

id = db.Column(db.Integer, primary_key=True)

Your First Full Model

Put it together: a key plus a couple of fields. This tiny class fully describes a User table and its columns.

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))

Override the Table Name

Want a custom table name? Set __tablename__ on the class and the ORM uses that instead of the default.

__tablename__ = "users"

Create Instances Like Objects

A model instance is just a normal Python object. You pass column values as keyword arguments, and each becomes a row to save.

u = User(name="Ada")

Add a Helpful repr

Defining __repr__ makes objects print clearly in the shell and logs, which saves you guessing during debugging.

def __repr__(self):
    return f"<User {self.name}>"

Models Live in One Place

Keep models in a models.py file so they import cleanly into views, scripts, and tests without circular imports.

No SQL in Sight

Notice you never wrote CREATE TABLE. The model declaration is the schema, and the ORM translates it to SQL for you.

Quick Check

What does a Flask-SQLAlchemy model class represent?

Recap

You built a model by subclassing db.Model, declared columns, set a primary key, and learned that the class itself is your schema. ✅

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

هل درس «تعريف أول فئة نموذج لك» مجاني؟

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

ماذا ستتعلم في «تعريف أول فئة نموذج لك»؟

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

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

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

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

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

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

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

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

  1. تثبيت الامتداد وضبطه
  2. تعريف أول فئة نموذج لك
  3. الأعمدة والأنواع والقيود
  4. إنشاء المخطط باستخدام create_all
← العودة إلى Flask Academy