Skip to content
Learn Netverks

Lesson

Step 18/36 50% through track

models-basics

Models basics

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Models basics: the syntax, APIs, and habits you need before advancing in Django.

The ORM is Django’s core productivity lever—N+1 queries and migration mistakes show up in every senior review.

You will apply Models basics in contexts like: Blog engines, e-commerce catalogs, and multi-tenant SaaS data layers.

Write Python 3 in the editor and click Run on server—the dev runner executes your script; Django framework lessons also use local startproject for full MVT (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Django models define your database schema as Python classes. One model class maps to one database table; instances map to rows. The ORM translates Python to SQL.

Defining a model

from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    published = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

Manager and objects

Article.objects is the default manager—entry point for queries: Article.objects.all(), .filter(), .get().

Important interview questions and answers

  1. Q: Model vs table?
    A: Model is Python class; Django creates/updates the table via migrations.
  2. Q: Primary key?
    A: Auto id BigAutoField unless you set primary_key=True on another field.
  3. Q: Raw SQL when?
    A: Rare—ORM covers most cases; use .raw() or cursor for reporting/performance edge cases.

Self-check

  1. What attribute accesses all rows?
  2. Which field type stores long text?

Interview prep

Model vs table?

A model class defines schema in Python; migrations create/update the actual database table.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Model = table?
  • Primary key default?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump