Django | Part 4 | Building Your First Model

Last edited on Sept. 18, 2022, 8:28 p.m. by James Morgan
Django  Python  Web Development 
...

Now that we have successfully set up a new Django project and gone through some core concepts, it is time to start building the website. This article will begin with setting up your first app and model. As mentioned in the preceding article, models are foundational components of the Django framework and are the primary source of data that powers the dynamic elements of a website. As this project is building a blog, it would make sense for the first app on the website to focus on that.

To achieve this, this article will run through the following four steps:

  1. Setting up a “blog” app
  2. Designing and building an “article” model
  3. Migrating the database changes
  4. Creating a superuser and interacting with the model in the Django admin console

1. Setting up a “blog” app

In either PowerShell or Terminal, activate your virtual environment before navigating to the Django project’s root directory. This will be the one with the manage.py file in it. Once there, run the Django startapp command as outlined below.

#Activate virtual environment and move to django root directory
venv/Scripts/Activate
cd blog_website
#Create blog app
python manage.py startapp blog

Inside your project directory, you will now see a new directory called “blog”. For this app to work correctly, it needs to be registered in the settings.py file. This can be done by opening the file, scrolling down to the “INSTALLED_APPS” variable, and adding “blog” to the array. After which, it will look like the following.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Other apps
    # Own apps
    'blog'
]

After saving the file, the blog app will have been successfully set up and registered under Django admin.

2. Designing and building an “article”model

With the “blog” app set up, it is time to create the first model within the app. As the primary content of a blog is its articles, this first model will be for the articles. For simplicity’s sake, it will be called “Article”. When setting up a model, it is essential to think about the use case and components that you will need to consider. Every article in this blog will sit in the database under this model. Therefore, the model needs to be structured so that the model fields meet the criteria of each article. For example, each article will need to have each of the following fields:

  • Title
  • Header image
  • Description
  • Slug (URL-friendly title)
  • Author
  • Time it was last updated
  • Time it was created
  • Status (draft or published)
  • Category

No doubt this list will be subject to change as websites’ needs grow. However, for now, this list will suffice. In a subsequent post, I will go through how to add or remove fields to a model, addressing the key considerations you need to know.

With the model fields defined, under the “blog” app directory, open up the models.py file to begin setting it up. First, at the top of the file, there is a need to import the different Python modules that the model will use. This also covers importing the prebuilt Django user model to be used as the “author” field. Below is the full list of required modules and what they will be used for:

  • django.db.models – This is all of the Django model field types.
  • django.contrib.auth.models.User – The prebuilt user model that comes with Django authentication.
  • django.urls.reverse - Easy to specify URLs.
  • ckeditor.fields – Allows for rich text fields.

Note that the ckeditor module has not yet been installed, so you will also need to append it into your requirements.txt file and install it using the pip install -r requirements.txt command in Powershell. You will also need to go into your settings.py file and add it to the “INSTALLED_APPS” array.

In this case, a class-based model will be used to define “Article” as it is well suited for the blog’s needs. To do this, make a Python class, then list each of the above-defined fields under the django.db.models. With each field, specify the field type and any other specific elements. There are a lot of different field types, but the key ones that will be used in this model are:

  • CharField: This is a text field generally used for smaller amounts of text.
  • TextField: This is another text field but used for more significant amounts of text.
  • SlugField: This is generally a short label for something; in this case, it will be the URL path.
  • ForeignKey: This is a relationship field used to link to another model; in this case, it will be for the author field linking to the “User” model.
  • DateTimeField: This is used to reference a timestamp or date.
  • IntegerField: This is used to reference a whole number.
  • RichTextField: This is a special field that is a part of the ckeditor module. It takes a rich text input, including all formatting.

Then at the end, add a small function using lazy reverse to get the absolute URL based on the slug. After all this, the models.py file should look like the script below.

from django.db import models
 
# Create your models here.
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from ckeditor.fields import RichTextField
 
# List of tuples for my status field
STATUS = (
    (0,"Draft"),
    (1,"Publish")
)
 
class Article(models.Model):
    title = models.CharField(max_length = 120)
    header_image = models.CharField(max_length = 2040, null=True, blank=True)
    description = models.TextField()
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='article_posts')
    updated_on = models.DateTimeField(auto_now= True)
    created_on = models.DateTimeField(auto_now_add=True)
    status = models.IntegerField(choices=STATUS, default=0)
    category =  models.CharField(max_length=255, default="None")
    content = RichTextField(blank=True, null=True)
 
    # This orders the data by the date the article was created 
    class Meta:
        ordering = ['-created_on']
 
    def __str__(self): # I add this to make the objects in the admin have a clean name
        return self.title + " | " + str(self.author)
 
    # Need to redirect from form
    def get_absolute_url(self):
        return reverse("article-detail", kwargs = {"slug": self.slug})

A new model must be registered in the admin.py file each time it is created. This is a simple line of code looking like the below:

# Register your models here.
from .models import Article
admin.site.register(Article)

3. Migrating the database changes

With the model and app now set up, the changes need to be migrated to the database. This can be done with the following two commands in either PowerShell or Terminal.

# Create and make database migrations
python manage.py makemigrations
python manage.py migrate

4. Creating a superuser and interacting with the model in the Django admin console

Lastly, you must be logged in as a superuser to use the Django admin console. To create a superuser, enter the following command and follow the instructions to enter a username and password.

python manage.py createsuperuser

After completing all the above steps, the model should be visible in the Django admin console when you run the server. To validate that everything is working as expected, activate the server, and navigate to your local host address’s “/admin” page. Once logged in, you should see the “Blog” app with the “Article” model listed under it. Whilst you can click on the model and use this admin console to create articles, it is not a great user experience. Future articles will explore how to develop new model entries without the admin console.

The next article will put this newly created model to good use. It will focus on the remaining Django components we covered in the previous article. Then it will reference the “Article” model and a couple of test entries in the template.

Comments
Upcoming Project Ideas
GA4 Deployment
Server Side Tracking
Web3 Dapp
Web Analytics Dashboards