Django Models
A model describes stored fields and relationships using Python classes. Django’s ORM uses that description to build queries and materialize model instances.
How it works
Fields have storage, validation and form implications, but not every rule runs during a plain save. A CharField’s max_length helps define the model contract; constraints belong in the database when they must survive every write path. Our Workshop links to a Venue, stores a unique slug and keeps a published flag. The model’s Meta ordering supplies a default presentation order, not a guarantee that unspecified database queries are naturally sorted.
Run in the supplied local Django project
This lesson uses Django 5.2.18 LTS with Python 3.12. Download the source downloader and run node download-project.mjs http://127.0.0.1:8000 with Node 22 while previewing locally. It creates a new bhylu-django-lab folder and refuses to overwrite an existing one. After deployment, the default base is bhylu.com; until then use your local preview URL.
Inside that new folder, create a virtual environment, install requirements.txt and run the complete lesson file shown below. Every lesson creates a fresh in-memory SQLite database and temporary upload storage. It does not edit the interactive lab’s db.sqlite3 or Bhylu’s database. The visible listing below is the lesson’s core code; its complete downloaded file includes the shared bootstrap.
Django does not run in Bhylu’s current browser playground. No Apache, MySQL or global PHP configuration change is needed. See the setup lesson for the full project and commands.
Example
Run .venv\Scripts\python.exe lessons\models.py from the downloaded project root. Download the complete lesson file; keep it under lessons/. The project’s shared models, forms, views and fixture are prerequisites, not a hidden online runtime.
field = Workshop._meta.get_field("title")
print(field.get_internal_type(), field.max_length)
print(Workshop._meta.get_field("slug").unique)
print(Workshop.objects.count())
Expected result
CharField 100 True 3
Model metadata exposes the declared contract, and the seeded database contains three workshop rows. Defining the Python class alone would not create that table; migrations establish and evolve the database structure.
Use Django’s versioned official documentation for exact API contracts and the deployment checklist before a separate release.