Models and Migrations
Define and migrate the database.
Django Models
A Django model is a Python class that defines the structure of a database table. Each attribute is a field. Models live in an app's models.py.
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
print('Post model defined')Field Types
Django provides many field types that map to column types.
CharFieldfor short text (needsmax_length)TextFieldfor long textIntegerField,BooleanField,DateTimeField
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
views = models.IntegerField(default=0)
print('Fields map to columns')All lessons in this course
- Projects and Apps
- Models and Migrations
- Views and URLs
- The Django Admin