-->

django models and database schema design

Before we start the design our database schema, we have to create our django project. Our project is "Library Management"(For quicker understanding).

Before we start development of any software project, we should know about basics of  software development life cycle.
We have six steps in every software development life cycle.
  • Requirement gathering and analysis.
  • Design(users flow, data flow & database schema design, etc).
  • Implementation or coding.
  • Testing.
  • Deployment.
  • Maintenance.
We are not going deep into the above steps. But, We will discuss about the requirements for our project "Library Management".

Library Management allows user to register & login into the system. User can search for book with details (name, author, etc.) and can see details of book(availability, author, etc). User can request the Admin(Library staff) for book. Admin will see the request of user and will approve/reject the book and  dispatch it to the users address. After approving the book user will receive it and later he will return it within the due date. After receiving the book from the user Admin will mark it as received.

Based on the above requirement we can derive the user flow & admin flow like below diagrams.

User's flow:
library management user flow diagram
Admin/staff Flow:


library management admin flow diagram



From the above user-flow diagrams we can observe that we have to store the following details
1. User profile
2. Books Information
3. Book register(who has taken book, when returned, due date etc.)

Question yourself what to store and why so that you will understand the usage of fields in models & use appropriate names for models.
1. User Profile:
  • first name, last name, email, is staff(to distinguish users admin, user), profile picture, address
2. Book Information:
  • title, author, description, image, available books(number)
3. Book register:
  • user(who taken book), book(which book), took on(date), returned on (date), due date
Following diagram shows the entity relationship model for our project. we have chose the data type based on the data.
database schema for library management django
 we have designed our database model. Our next step is to implement it in our django project. Django is providing us "User" model with basic fields (first_name, last_name, email, is_staff, is_active, username, is_superuser). But we need to add extra fields like address and profile picture.
So, we have to customize the user model. In order to do that we have to configure the settings.py
AUTH_USER_MODEL = "library.User"
Now we are ready to write models, open the file "library/models.py" and write models init.

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


class User(AbstractUser):
    profile_pic = models.ImageField(null=True)
    address = models.TextField(null=True)

    def __str__(self):
        return self.email


def get_upload_file_name(book_object, file_name):
        return "%s/%s/%s" % ("Book", book_object.id, file_name)


class Book(models.Model):

    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    description = models.TextField()
    image = models.ImageField(upload_to=get_upload_file_name)
    available = models.IntegerField(default=0)
    ISBN = models.CharField(max_length=50)

    def __str__(self):
        return self.title


class BookRecord(models.Model):
    user = models.ForeignKey(User)
    book = models.ForeignKey(Book)
    took_on = models.DateTimeField(auto_now_add=True)
    returned_on = models.DateTimeField(null=True)
    due_date = models.DateField()

    def __str__(self):
        return str(self.user)
Now, follow the below steps to create database
python manage.py makemigrations
This command will create the migration files in the "library/migrations" directory.
python manage.py migrate
This command will run the migrations on django orm and creates the database.
Now, our database is successfully created and ready to use it.

Django provides defiirent kinds of model fields like "CharField", "IntegerField", "Foreign Key", "ManytoMany", "Decimal Field", etc.
Based on the type of data that we are storing in the attribute we have to choose the fields.
In User model, for attribute profile pic I have chosen the "ImageField" because user will upload the image.
In BookRecord model, I have used "ForeignKey" field for attribute user because I'm making the relation between User table to BookRecord table.
Like this we have many other fields we have to choose the fields for models attributes based on the storage data. Read the django official documentation  on model fields. Still if you don't understand please comment below, so that I can clarify your doubts.

Buy a product to Support me