In this section, we will implement our blog application as well as a Django model called Post, which contains the object-oriented logic for the posts in our blog:
- First, in the manage.py panel, enter the following command:
manage.py@mysite > startapp blog
- This, as we have seen, will create a subfolder within our current project directory named blog with all the various Django-specific Python scripts for our blog application. Now, within the blog/models.py file, enter the following code (again, manually typing in the code is highly recommended):
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
STATUS_CHOICES = {
('draft', 'Draft'),
('published', 'Published'),
}
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
unique_for_date='publish_date')
author = models.ForeignKey(User, related_name='blog_posts',
on_delete=models.DO_NOTHING)
body = models.TextField()
publish_date = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices=STATUS_CHOICES, default='draft')
class Meta:
ordering = ('-publish_date',)
def __str__(self):
return self.title
This Post class (which inherits from the models.Model class in Django) implements the model for our post objects.
While most of the preceding code is self-explanatory, there are a few things we should note within the model:
- All of the attributes of the Post class (for example, title, slug, author, and so on) are implemented by Django's built-in classes. Once again, we can see the powerful features that Django offers when it comes to implementing complex data structures for our web applications.
- We haven't seen the models.SlugField class before in this book. This class offers a way to generate a valid and readable URL from other attributes of a specific class instance. We will look at an example of this feature later on in this chapter when we launch this blog application.
- The author attribute is a foreign key referencing the built-in User model of Django.
- Finally, in the Meta class, we specify that we want the order of these Post instances to be descending (using the minus sign) with respect to the publish_date attribute so that newly added instances will appear first.
- Next, we need to register this new blog application with our main project. To do that, locate the INSTALLED_APPS list variable in the mysite/settings.py file and add the 'blog' string to that list. The variable should now look as follows:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog'
]
- Finally, we will apply the relevant migrations and create a corresponding database table for our new model. From the manage.py panel, run the following commands sequentially:
manage.py@mysite > makemigrations blog
manage.py@mysite > sqlmigrate blog 0001
manage.py@mysite > migrate
While executing these commands, you will see corresponding SQL statements being used to generate our table in the printed output of the manage.py panel. With everything executing successfully, our blog application has been officially created and set up.