Django — Step 7

Database Model Shema

Brian Mayrose
Jul 11, 2019 · 3 min read

We are continuing from step 6

Here is the reference documentation provided by Django for models:

With the display_1 app we created, we need to design a database model for the items we are going to display.

For this model, we will need:

  • The title of the item
  • A category that it might belong to
  • A quick description
  • A more in-depth description
  • Additional related information
  • External reference links
  • The main image
  • Multiple supplementary images
  • Date of upload of item

Django doesn't have Image uploading capabilities built-in by default, but the Pillow module does. Let's install pillow via pip:

pip install pillow

Now we can create this model, open the display_1/ file

At the top of the file below the from django.db import models line import datetime

from django.db import models

then we can put our item model below that:

class item(models.Model):
title = models.CharField(max_length=200)
description_1 = models.TextField(blank=True)
description_2 = models.TextField(blank=True)
description_3 = models.TextField(blank=True)
reference_link = models.TextField(blank=True)
photo_main = models.ImageField(upload_to='photos/%Y/%m/%d')
photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
photo_3 = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
photo_4 = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
photo_5 = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
photo_6 = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
is_published = models.BooleanField(, blank=True)
list_date = models.DateTimeField(, blank = True)
def __str__(self):
return self.title

We are defining a class titled item. We have defined a title, 3 description areas, an external reference link, The main photo, 6 supplementary images, the listing date, and a boolean option we will use to publish and unpublish the item.

the Django documentation explains in detail the options we use for the definitions. CharField, TextField, ImageField, BooleanField, and DateTimeField all have different requirements in their use.

at the end of the class, is another definition:

def __str__(self):
return self.title

This tells the Django admin what definition to display, in this case, it is the title.

Save the file and from your terminal run the makegrations command:

python makemigrations

If there are any misspellings or other errors the makemigrations command will let you know. A brief description and a path to a new migration file added to the migrations directory is what is returned in the terminal if everything goes correctly.

Then we run the migration to the database:

python migrate

If you make any changes to the model in the future, just run The makemigrations and migrate commands again and the model will be adjusted in the database.

You can use DBeaver and view your newly defined model

djdemo > Shemas > public > display_1_item

Now is also a good time to delete the db.sqlite3 file in your project root directory. This is the default database file provided by Django and we will not be using it.

In the next step, we will explore the admin area Django provides and also upload some content to the database.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade