Akshay Gaur

Starting a new application

To start and application, fire up the terminal (make sure you are in the directory where manage.py resides) and type the following command:

(venv)$ python manage.py startapp students

If you look at the folder structure again, you will find that a new folder was added:

tutorial
|-- manage.py
|-- students
|  |-- admin.py
|  |-- apps.py
|  |-- __init__.py
|  |-- migrations/
|  |-- models.py
|  |-- tests.py
|  `-- views.py
`-- tutorial
  |-- __init__.py
  |-- settings.py
  |-- urls.py
  `-- wsgi.py

Don't worry about the migrations folder, we will get to it in just a minute.

RECAP: We created a project named tutorial. Ran a server to see that the project is serving the pages. Then, we created an application which is the logical equivalent of a subset of your web application with a specific function. In this case, we will use the students app to manage the students in our system.

Now, lets create a model for our app.

What is a model?

A model is a representation of an entity that we will store in a database. So, for instance, if we created a model for a car, we would want to store how many doors it has, what is engine capacity, horsepower that it generates, maximum torque, its purchase price etc. You can most certainly imagine many more attributes for this entity that you would want to store. Creating a model allows us to save these details about the entity in the database.

For our application, we want to store students as entities. The details that will be required in an actual production environment would depend on the requirements. For us though, we will keep is simple. We will store the following attributes for the students:

We would also like to define an entity named course which would have the following attributes (keeping it simple, could have a lot of details apart from these details):

Finally, we would like to store our departments as a separate entity:

Now, we need write the code to define these models so that our project understands what each entity can store and there is any relationship between the entities.

Open up the models.py model in students folder and define the department model:

from django.db import models
      
      
class Department(models.Model):
    """
    This model will store the details about our department.
    Primary key for this model is department code.
    """
    class Meta:
        db_table = "departments"
        ordering = ['d_name']

    d_name = models.CharField(max_length=200)
    d_code = models.CharField(max_length=5, primary_key=True)
    d_chair = models.CharField(max_length=200)

    def __str__(self):
        """
        String representation of the department.
        This will be used mainly when we print
        a department object.
        """
        return self.d_name

Now we will try to understand what we did in defining the model for the department:

Line 1: Declared the model name, and that it will inherit from the standard django model as defined in models.Model.

Lines 2-5: Comments.

Lines 6-8: We defined some meta information about the model. Namely, "db_table" tells django to store the model in a table named "departments". If we didn't define this, the table name in our database would be some thing that would not be fun to read. Then, we also defined what is the default order of this model. Note that these commands will be translated to corresponding db commands (SQL commands).

Lines 10-12: We defined the actual attributes of this model. They will all be character fields (a default type provided by django framework). We also defined the length of these fields.

  Also note that we defined a primary key for the model by passing a keyword argument "primary_key=True" in the d_code attribute.

Lines 14-20: We defined a string representation for the model whenever the system needs to represent an object model as a string for myriad reasons. If you are using Python version 2.x, it is suggested to used __unicode__ instead of __str__.

So now that we have the model with us, how do we start saving it in the database?

For that, first we need to tell django where to start storing the data itself. I am assume here that the database that we will use in the following sections is already created. If you don't want to use postgres as your db of choice, that is fine as well.

First, let us go to our settings.py file in our 'tutorial' folder and find 'DATABASES' entry. It would look something like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

What this means is that as default, django sets up a sqlite3 database in the base directory of the project and the name of the database is db.sqlite3. We are going to change it to postgres (or some other db, it is up to you).

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'tutorial_db',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

We are mostly done here. We just need to tell the project about this new 'students' application of ours. In the settings file that we are in, find 'INSTALLED_APPS" and add our application name to the existing list:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'students',  # Add your app name here.
]

Now we will ask manage.py to prepare the db queries that it needs to make in order to create the required tables in the db that we provided it the path to in "DATABASES" settings.

To do that, we run the following:

(venv)$ python manage.py makemigrations students

This will create all the necessary db queries to setup our db. Now we will execute those queries using:

(venv)$ python manage.py migrate

You should see messages where it applies all the migrations in the db successfully.