Part 5 - Models in Django

November 9th, 2020 4 min read

This is part of the series Django For Beginners we are going to see about apps in app and we will see about models in django in this part.

What are models ?

Models play an important role in django, it makes it what it is. Models is the way to represent our database in a pretty form. We can also change databases without any changes to models in django, and we use these models to query our database. Models make life easy.

We define models for our application in apps in models.py file. Now let’s see the example, in this example we are going to design a model for a blog post and tags associated with blog post. In real-life scenario you can also you libraries such as django-taggit for tagging the blog post and to do the heavy lifting for us. Now let’s see the example,

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

class Blog(models.Model):
	title = models.CharField(max_length=250)
	post  = models.TextField()
	slug  = models.SlugField()
	tags  = models.ManyToManyField('Tag', blank=True)
	createdAt = models.DateTimeField(default=timezone.now)
	createdBy = models.ForeignKey(User, on_delete=models.CASCADE)

class Tag(models.Model):
	tag = models.CharField(max_length=250)
	createdAt = models.DateTimeField(auto_now=True)
	blogs = models.ManyToManyField(Blog, blank=True)

In above code we have defined two models, we define models by creating the classes with name which we want the name for model and then we inherit from Model class from django.db.models. Each class represents a table in a database. Now here I have only covered the most used fields you can check a huge variety of the models fields on the Django’s official documentation page for models I will link it in the end.

Some Basic Fields

Now let’s check functionality of each of the field. But before doing that let me tell you how fields word in django. We define field be creating a variable with the name of the field and then setting it attribute of the model representing a database field. Functionalities of these field is as follows,

  1. CharField : It is normal text field with a fixed text limit set with max_length keyword argument.
  2. TextField : It is text field with unlimited value for text and if text is too large to be stored in database then the data will be stored in a separate text filed and all the stuff would be managed by database itself.
  3. SlugField : It is filed which is used to store slug. Slug are web safe char.
  4. DateTimeField : As the name suggests it is used to store date time field. It can be set with a default value like timezone.now from django.utils will give the value of the time at the time that model is created. We can also use auto_now this will produce the same effect.
  5. ForeignKey : It is used to produce One To One relation in database.
  6. ManyToManyField : It is used to produce Many To Many relation in database.

Note : All the validation is build in these fields. We can also customize the fields by passing the keyword arguments to the field.

Registering Models in Admin Panel

Now let’s see this in admin panel of our application. To do this we need to add this to the admin panel and we can do this in the following way,

from django.contrib import admin
from .models import Blog, Tag

admin.site.register(Blog)
admin.site.register(Tag)

We register models to admin interface by first importing them in admin.py of the model in the app they are made, and then we register with the following syntax admin.site.register(modelName). You can visit django’s admin interface and see it, here are some images when we of the model in create new entry mode, for each model.

Create New Mode for Blog model in Django Admin Interface

Create New Mode for Tag Model in Django Admin Interface

I wrote “create new entry mode” above as I am not sure what to write it as, and here are the link to All Fields in Django and I suggest you taking a look at the documentation.

Hope you like it and be sure to checkout complete series.


You may also like: