Python Django Tutorial - Django Models: Creating A Model
Python Django Tutorial - Django Models: Creating A Model
We have discussed how to create a django project and django app in our first Django tutorial. In
this tutorial we will talk about django models. We will cover the following topics:
Creating a model.
Registering Model in django admin site.
'Class Meta' in django models.
Creating Objects of Models
__str__ Method in django models.
get_absolute_url() method of django models.
@property decorator in django model.
Django Model Fields
Model is a structure for storing data. The model handles everything related to data. Such as: data
access, data validation, dealing with data of other models, etc. It mirrors the fields and behaviors
of the data. Generally a model is implemented as a python class which is a subclass of
django.db.models.Model. Django gives an automatically-generated database-access API to
create a table in our database by each model, and the "attributes" of the table are created by the
"fields" of the model class. Models are defined inside an app’s ‘models.py’ file.
After creating a project and an app & before starting writing codes in your first_app/models.py
make sure you have these three things:
Creating a model.
In your first_app/models.py
class Person(models.Model):
GENDER = (
('M', 'Male'),
('F', 'Female'),
)
#Fields
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(unique = True)
After creating this model we will have to run migrations commands to convert these into database table:
python manage.py makemigrations
python manage.py migrate
Now, if you go to admin panel: http:127.0.0.0:8000/admin (don't forget to run your server first; running
the command 'python manage.py runserver'). You will be asked for admin username & password.
Give the credentials of the user that you created using 'python manage.py createsuperuser'.
from django.contrib import admin
from first_app.models import Person
# Register your models here.
admin.site.register(Person)
'Class Meta' in Django Models.
See, We have named the model as ‘Person’ but Django is showing us the plural form “Persons”. Nothing
seems wrong with that but think that you have a model named ‘Category’ Django will show that as
“Categorys” in admin panel where it should be "Categories" in plural. To avoid this problem, we will
have to write a Meta class in which we will define the model’s name in plural for us:
#Metadata
class Meta:
Add this piece of code inside your model class & refresh your admin panel. You will see the output:
This is not the end. You can also use more metadata like (verbose_name, ordering, app_label, db_table
etc ) about your 'Model' in Meta Class.
#Method
def __str__(self):
return self.first_name + self.last_name
#methods
def get_absolute_url(self):
"""returns the url to access an individual object of the model."""
return reverse('person-detail-view', kwargs={'pk':self.pk})
When it comes to web development you really want to avoid hard coding paths in your templates. The
reason for this is that paths might change, and it will be a hassle to go through all your HTML and
templates to find every single URL or path and update it manually. It makes your code much harder to
maintain.
<a href="{{person.get_absolute_url}}">Link</a>
@property
@property is a built-in decorator in Python. @property decorator is just an easy way to call
the property() function, Which is used to return the property attributes of a class. In our model we took
first_name and last_name. Now if we want a users full_name then joining the first_name & last_name
would be enough. So, we will right a function to join them...
@property
def full_name(self):
return f'{self.first_name} {self.last_name}'
This means you can call full_name as if it were a member variable instead of a function. Like
'person.full_name' instead of 'person.full_name()'
We can see the output in our admin panel. Add the following codes in admin.py.
class PersonAdmin(admin.ModelAdmin):
list_display = ['full_name','email','gender']
# Register your models here.
admin.site.register(Person, PersonAdmin)
Now, you will see the ‘full_name’ in your admin panel along with email & gender. Though email &
gender are member variables and ‘full_name’ is a method.
from django.db import models
from django.urls import reverse
# Create your models here.
class Person(models.Model):
GENDER = (
('M', 'Male'),
('F', 'Female'),
)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(unique = True)
gender = models.CharField(max_length=1, choices= GENDER)
class Meta:
verbose_name_plural = 'All Persons'
@property
def full_name(self):
return f'{self.first_name} {self.last_name}'
def __str__(self):
return f'{self.first_name} {self.last_name}'
#methods
def get_absolute_url(self):
"""returns the url to access an individual object of the model."""
return reverse('person-detail-view', kwargs={'pk':self.pk})
Django Model Fields
In our example we have used CharField & EmailField. But there are different types of fields in django.
Here are some which are mostly used Django Model Fields:
models.NullBooleanField() Boolean Works just like BooleanField but also allows NULL
values
models.DateField() Date/time Creates a date field to store dates
models.TimeField() Date/time Creates a time field to store times.
models.DateTimeField() Date/time Creates a datetime field to store dates with times
models.DurationField() Date/time Creates a field to store periods of time.
models.AutoField() Number Creates an integer that autoincrements, primarly used
for custom primary keys
models.DecimalField() Number Stores a number. Takes two arguments;
(decimal_places= X, max_digits=Y )
models.FloatField() Number Stores floating-point numbers.
models.IntegerField() Number Stores integer numbers.
models.PositiveIntegerField() Number Works just like IntegerField but limits values to positive
numbers from 0 to 2147483647
models.CharField(max_length=N Text Creates a text column, where the 'max_length' argument
) is required to specify the maximum length in characters.
models.ImageField() Text Used for handling image files (e.g. getting the height &
(Specialized) width) Works just like
Note: this field requires the Pillow Python library (e.g.
pip install Pillow).
models.SlugField() Text Makes a string is a slug string, Slug is a concept that's
(Specialized) typically used to cleanse URL strings that contains
spaces and other potentially invalid character like letter
with accents.