Created
April 16, 2019 17:09
-
-
Save rgov/32acd7005c175fed68e86a82d58d5b81 to your computer and use it in GitHub Desktop.
crash course in managing a database with django-admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See also: | |
# https://docs.djangoproject.com/en/2.2/intro/tutorial01/ | |
# Install Django | |
python -m pip install django | |
# This creates a blank new Django project in mysite/ | |
django-admin startproject mysite | |
cd mysite/ | |
# Create a new app within your project. An app is just a way to separate | |
# different bits of functionality of your app. You can use one monolithic app | |
# for now. | |
python manage.py startapp garage | |
# By the way, that `manage.py` script is your CLI for managing your Django | |
# project. It has a bunch of useful features. | |
# Before we look at the garage app, we have to tell the overall Django project | |
# to use it. This is how you install any functionality, whether it's your own | |
# code or some module you found online. | |
nano mysite/settings.py | |
# Find INSTALLED_APPS and add 'garage' to the list. Save, close. | |
# Ok, now we can add a model. A model is a Python class where we define the | |
# fields and Django automatically does all the database stuff for us. | |
# | |
# There are a ton of different built-in field types. Good ones to understand | |
# are ForeignKey, OneToOneField, and ManyToManyField, since those let you define | |
# relationships. | |
# | |
# https://docs.djangoproject.com/en/2.1/ref/models/fields/ | |
# Edit garage/models.py and add: | |
class Car(models.Model): | |
make = models.CharField(max_length=128) | |
model = models.CharField(max_length=128) | |
plate = models.CharField(max_length=32) | |
# Now we'll hook this up to django-admin so that we get a web UI for managing | |
# the cars in our garage for essentially free. You can do a lot of customization | |
# of this, but we'll start simple. | |
# Edit garage/admin.py and add: | |
from .models import Car | |
admin.site.register(Car) | |
# Ok, that's all for the code. Now we need to tell Django our database model | |
# has changed. This automatically generates scripts that migrate a database from | |
# one version of the schema to the next. | |
python manage.py makemigrations garage | |
# That step created the migration scripts, but didn't run them. To run them | |
# locally and create a temporary database, it's just | |
python manage.py migrate | |
# Ok, last thing is we need an admin user we can log in with. This will prompt | |
# you for the username and password and insert the user record into the DB. | |
python manage.py createsuperuser | |
# Spin it up | |
python manage.py runserver | |
# Ok, now visit the admin site in your browser. | |
open http://localhost:8000/admin/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment