Adding models to Django
Let's enhance our project a bit
Before we dive into connecting our Django project to the RDS instance, let's enhance our app a bit.
We'll create a simple journal that documents our journey learning to play the Guitar. This will illustrate how Django can connect to the database. You can use these instructions to enable your own Zappa-powered Django project to connect to your RDS instance.
Add the models#
First, let's activate the guitar
Django application in the project. We added this app way back when we first set up this Django project, and now we're going to use it.
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"guitar",
]
Next, let's add a model to the guitar
app:
from django.db import models
from django.utils.timezone import now
class JournalEntry(models.Model):
title = models.CharField(max_length=256)
timestamp = models.DateTimeField(default=now)
content = models.CharField(max_length=1024)
class Meta:
verbose_name_plural = "Journal Entries"
And enable the model for the Django admin:
from django.contrib import admin
from .models import JournalEntry
@admin.register(JournalEntry)
class JournalEntryAdmin(admin.ModelAdmin):
list_display = (
"title",
"timestamp",
"content",
)
Don't forget to push these changes to the live dev
environment:
(ve) zappashell> zappa update dev
Initialize the database#
To initialize the database, we have to instruct Django to create migration files:
(ve) zappashell> python manage.py makemigrations
You should see:
(ve) zappashell> python manage.py makemigrations
Migrations for 'guitar':
guitar/migrations/0001_initial.py
- Create model JournalEntry
(ve) zappashell>
Then let's insert these models into the database. Note we are running this command from the local machine, but we could also use Zappa to invoke the same management commands. Doing it locally saves us from AWS charging us for AWS Lambda time. However, doing from AWS Lambda might be your best option if the database is not publicly accessible.
(ve) zappashell> python manage.py migrate
You should see:
(ve) zappashell> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, guitar, 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 admin.0003_logentry_add_action_flag_choices 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 auth.0009_alter_user_last_name_max_length OK
Applying auth.0010_alter_group_name_max_length OK
Applying auth.0011_update_proxy_permissions OK
Applying auth.0012_alter_user_first_name_max_length OK
Applying guitar.0001_initial OK
Applying sessions.0001_initial OK
(ve) zappashell>
This page is a preview of Serverless Django with Zappa