Designing Your Model
Weāve now arrived at the core of your Django-based blog application: the models.py file. This is where weāll define the data structures of the blog. Following the principle of Donāt Repeat Yourself (DRY), Django gets a lot of mileage out of the model information you provide for your application. Letās create a basic model, and then see all the stuff Django does for us using that information.
Open up models.py in your favorite text editor (bonus points if it has a Python mode with syntax coloring). You see this placekeeper text:
from django.db import models # Create your models here.
Delete the comment, and then add the following lines:
class BlogPost(models.Model): title = models.CharField(max_length=150) body = models.TextField() timestamp = models.DateTimeField()
Thatās a complete model, representing a āBlogPostā object with three fields. (Actually, strictly speaking it has four fieldsāDjango automatically creates an auto-incrementing, unique id field for each model by default.)
You can see our newly minted class, BlogPost, is a subclass of django.db.models.Model. Thatās Djangoās standard base class for data models, which is the core of Djangoās powerful object-relational mapping system. Also, you notice our fields are defined like regular class attributes with each one being an instance of a particular field class. Those field classes are also defined in django.db.models, and there are many more typesāranging from BooleanField to XMLFieldāthan the three weāre using here.