Beginner
What are Django models and how do you define them?
Django models are Python classes that define the structure of your database tables. They serve as the bridge between your application and the database, allowing you to create, retrieve, update, and delete records easily.
To define a model, you create a Python class that inherits from django.db.models.Model
. Each attribute of the class represents a database field. For example:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
published_date = models.DateField()
In this example, the Book
model has three fields: title
, author
, and published_date
. After defining your model, you can create a migration with python manage.py makemigrations
and apply it with python manage.py migrate
.