A data model is the common characteristics of a type of person, object, or animal. For example the common characteristics of student is name, age, gender, grade, family status etc. In Django term, “Data Modeling” is the act of shaping or defining the common characteristics of a type of person, animal, or object. And there is no rule for that, everyone has her/his own way to characterize a type of person and/or object. For example, some persons will define the common characteristics of car by its color, its brand, its engine etc. Other people will define the common characteristics of car by its wheels, its size, its speed etc.
In fact, the common characteristics or the data model of multimedia object could be defined by its text content, video content, title, published date, picture, category, author, and its unique id. In Django web framework, to define a data model, we must first define a class that is the subclass of the django.db.models.Model.
#home/models.py
from django.db import models
from django.conf import settings
# Create your models here.
class Media(models.Model):
id = models.CharField(primary_key=True,max_length=500)
title = models.CharField(max_length=500)
content = models.TextField(max_length=500)
thumb = models.CharField(max_length=500)
video = models.CharField(max_length=500)
category = models.CharField(max_length=500)
date = models.DateTimeField()
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
We see that our data model about multimedia or Media object in short is defined as Media subclass from django.db.models. All class attributes in the Media class represent the characteristics of Media object. And each characteristic called “field” is an instance of a class or datatype in Django web framework.
Before doing anything, we need to modify our ‘home’ app in INSTALLED_APPS section in settings.py module as below:
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home.apps.HomeConfig',
]
...
HomeConfig is the name of the class for the configuration in apps.py module.
From this Media data model, Django will create a table in Heroku's database with a schema mapping to this data model. Django will also create a form in its dashboard to get data about media from user. However, for Django to create a table to store media object in Heroku database, we need to make migrations and migrate first.
$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, home, sessions
Running migrations:
Applying home.0001_initial... OK
If we check in migrations folder, we will see a migration file was created in that folder.
# Generated by Django 4.0.5 on 2022-06-06 08:19
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Media',
fields=[
('id', models.CharField(max_length=500, primary_key=True, serialize=False)),
('title', models.CharField(max_length=500)),
('content', models.TextField(max_length=500)),
('thumb', models.CharField(max_length=500)),
('video', models.CharField(max_length=500)),
('category', models.CharField(max_length=500)),
('date', models.DateTimeField()),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
To let Django create a form for our media object in the dashboard, we need to register our Media data model in admin.py module.
from django.contrib import admin
from .models import Media
# Register your models here.
admin.site.register(Media)
We see that after a data model was created, Django creates a form mapping to that model in the dashboard, it also provides the CRUD (Create, Read, Update, Delete) functionalities to work with that model. So, with Django, what we have to do is only creating data model.