What is a django model?
A model is a class that matches a database table (or collection).
Models are defined in the apps models.py file, /app/models.py.
Say what? Let’s make an app with a database model!
Models example
We start a new project, movie database.
Inside the directory movie_site we now see:
Configure database
Open movie_site/settings.py, there are the lines:DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
A database is already set up!
We’ll use the default database system: sqlite3.
Note: Django supports many databases including:
- SQLite (django.db.backends.sqlite3)
- MySQL (django.db.backends.mysql)
- MongoDB (django_mongodb_engine)
- Oracle (django.db.backends.oracle)* PostGreSQL (django.db.backends.postgresql_psycopg2)
- NoSQL DB
Create your database with this command:
Now you’ll see lots of lines:
“
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying sessions.0001_initial… OK
Good job!
Create an app
Django sites can have many apps. Each app is like a plugin.
Let’s make a movie details app.
“
You have just created an app!
There’s a new folder: movies.
Apps are not added by default.. Let’s teach django.
Open movie_site/settings.py.
Scroll down to the line INSTALLED_APPS and add the line ‘movies’.
Create a movie model
We can create our model now. In the movies/models.py you can define all models.
Create a class for your movies model. The class will map to the database table.
If you don’t know classes and object orientated programming, take this couse:
We class will contain attributes of our movie object.from django.db import models
# Create your models here.
from django.db import models
from django.utils import timezone
class Movie(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
Run the command;
This will output:
movies/migrations/0001_initial.py
- Create model Movie
Also type this command:
The magic continues:
Apply all migrations: movies
Running migrations:
Applying movies.0001_initial… OK
In English, that means it’s just created our database table!
Browse database
Open the database file (db.sqlite3) with sqlitebrowser.
Yeehaw! Our movie model is now our database.