Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Last active May 18, 2017 09:45
Show Gist options
  • Save prodeveloper/289735b5b922653976a43a8587bcd089 to your computer and use it in GitHub Desktop.
Save prodeveloper/289735b5b922653976a43a8587bcd089 to your computer and use it in GitHub Desktop.
Working with Django models

In this class, we shall be interacting with Django's models. We shall see how we can create a data structure that then we can modify data in by using the built in admin panel

##Step 1

To start with we need to give our app firstapp a more meaningful name. Let’s say “akirachix”.

Update your settings file to reflect the new app.

Django defines its models as

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

A model then allows us to define how our data will look like and Django will then ensure its integrity.

Once our model is built we can use it to store and retrieve data from the database. This saves you from doing a lot of SQL and makes your code that much more easier to maintain.

Let us look at how we would fulfill this requirement using Django models

As Linda, I want to be able to store the following information about new students: Names, Course and Year they were with us.  

We start by first creating the model in our models.py file.

This model will now be called akirachix.student. This is because it inherits from the module akirachix

##Step 3 Django models allows us to define the structure of the database. However to be able to apply the changes over time, we need Migrations. Migrations are scripts that allow us to change the structure of the database over time. Trust me the changes will come!

We usually run a migration when one of the following has happened:

  • A new model, such as Student is created
  • A new field is added to an existing model
  • An existing field is removed from a model
  • When an existing field is modified

Migrations are done in two steps:

  1. python manage.py makemigrations
  2. python manage.py migrate

The first command creates the migration script, which is the set of steps that django will need to take to match your database to your models file.

The second command runs the script. You must run both commands.

The commands are run from your terminal where the manage.py file is

Let us first create our migration

Now lets run the migration.

##Step 4

While now we have structure in the database, we still don’t have a way of accessing it. The easiest way to start is to use Django’s admin panel.

To do so we need to register our model in the admin file.

The admin.py file is inside the akirachix folder. Modify it to import the model that we just built.

##Step 5

You can now login to the admin side. You will note that students now also appears.

You can now easily add and remove data to your model

##Assignment

Using the knowledge you have gained. Write code to fulfill the following requirements.

    As Linda, I want to be able to add edit and delete courses using django admin. course has the following attributes: title, location, trainer

##References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment