Django Tutorial 3 – MyNotes App

Now, Let’s try to create our models using Entity Relation diagram. Below is the model file. We need to edit the file named inside Django app folder. Copy the below content to file.

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class mynotes(models.Model):
    note_id = models.AutoField(db_column='note_id', primary_key=True)
    note_text = models.TextField(db_column='note_text', max_length=2000, null=False, blank=False)
    added_date = models.DateTimeField(auto_now_add=True)
    status = models.TextField(max_length=10,null=False,blank=False)

class tags(models.Model):
    tag_id = models.AutoField(db_column='tag_id', primary_key=True)
    tag_name = models.TextField(db_column='tag_name', null=False, blank=False)
    added_date = models.DateTimeField(auto_now_add=True)
    added_by = models.ForeignKey(User,on_delete=models.CASCADE)

class note_tags(models.Model):
    note_tag_id = models.AutoField(db_column='note_tag_id', primary_key=True)
    note_id = models.ForeignKey(mynotes, on_delete=models.CASCADE)
    tag_id = models.ForeignKey(tags, on_delete=models.CASCADE)

I have used few Django model fields which are required. You can read about the model fields further from this official documentation (Click Here). Below are the explanation about the model fields which we use for mynotes app.

  1. AutoField with primary_key attribute will override the default “id” column creation for the model. Whatever the key you mention here will become the primary key for the model.
  2. TextField is to store the varchar value in the database column where you can mention the max and min size of the column.
  3. DatetimeField is to store the date and time value for the note and auto_now_add attribute specifies to store the datetime value while inserting the records only where auto_now attribute updates the current datetime value whenever the record gets updated. auto_now attribute we will be using where modified_date is our requirement.
  4. ForeignKey is to mention what table to be referred for that particular column and on_delete attribute specifies what should be the action when the referred table entry get’s removed.

Our configured database is SQLite here. Let’s populate the Django admin related models to DB first. For that, run the below command.

(djangoenv) arockia@arockia-pc:python makemigrations
(djangoenv) arockia@arockia-pc:python migrate
(djangoenv) arockia@arockia-pc:~/Scripts/Python/eazytutor_work/mynotes/mynotes$ python migrate
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 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 sessions.0001_initial... OK
(djangoenv) arockia@arockia-pc:~/Scripts/Python/eazytutor_work/mynotes/mynotes$ 

After the migrate, you can go and check the SQLite database file for the migrated tables.

Figure1. Database Tables after initial migrate

Now, Run the below command to start the development server.

(djangoenv) arockia@arockia-pc:~/Scripts/Python/eazytutor_work/mynotes/mynotes$ python runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
November 22, 2019 - 11:49:55
Django version 2.2.7, using settings 'mynotes.settings'
Starting development server at
Quit the server with CONTROL-C.

Development server is running now in your machine’s local loopback IP with the port 8000 by default. Try to open the link in your browser. You can see the similar output as below.

Figure 2: Django development server output

Now, we need to create the models for our application notesapp. Run the below command

(djangoenv) arockia@arockia-pc:~/Scripts/Python/eazytutor_work/mynotes/mynotes$ python makemigrations notesapp
Migrations for 'notesapp':
    - Create model mynotes
    - Create model tags
    - Create model note_tags
(djangoenv) arockia@arockia-pc:~/Scripts/Python/eazytutor_work/mynotes/mynotes$ python migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, notesapp, sessions
Running migrations:
  Applying notesapp.0001_initial... OK
(djangoenv) arockia@arockia-pc:~/Scripts/Python/eazytutor_work/mynotes/mynotes$ 

Now, All the relevant tables got created in SQLite database.

Figure 3: Notesapp models creation

Now, we need to prepare our Django Admin panel to interact with our model first. run the below command to create the Django admin login credentials.

(djangoenv) arockia@arockia-pc:~/Scripts/Python/eazytutor_work/mynotes/mynotes$ python createsuperuser
 Username (leave blank to use 'arockia'): admin
 Email address:
 Password (again): 
 The password is too similar to the username.
 This password is too common.
 Bypass password validation and create user anyway? [y/N]: y
 Superuser created successfully.
 (djangoenv) arockia@arockia-pc:~/Scripts/Python/eazytutor_work/mynotes/mynotes$ 

Above command will create admin panel with username admin and the password admin123. Since my password is not secured and it has the username in it, Django alerts us while creating that credential but still we can override and create it. Creating such passwords in production environment is strictly not advisable due to security risks. Now, let’s validate the admin credentials by login to admin console using this URL (http://server/admin). See the below screenshots.

Figure 4: Django Admin console Login
Figure 5: Django Admin Console page

Let’s see how to add our models to this admin page to manage. To do that, we need to update script with the below content.

from django.contrib import admin
from notesapp import models

# Register your models here.

After adding this code to script, Django admin will understand that it should include these models as well in admin panel. See the below output.

Figure 6: Django Admin Panel with Notesapp

Now you can manage your model via admin panel also.

We have succesfully created and deployed our models to database and configured in Admin Panel as well. Now, We have our database tables ready to insert the data. In next lesson, we will prepare the HTML,CSS template. So we can first include the template to Django project before writing the CRUD (Create,Read,Update,Delete) code in our

Leave a Reply

Your email address will not be published. Required fields are marked *