0% found this document useful (0 votes)
165 views

Python Django Tutorial - Django Models: Creating A Model

The document discusses Django models. It covers creating a model class that subclasses django.db.models.Model, registering models in the Django admin site, using Meta options like verbose_name_plural, creating model instances, implementing __str__() and get_absolute_url() methods, using the @property decorator, and different types of Django model fields like CharField and EmailField. The goal of models is to structure and manage data in the database.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
165 views

Python Django Tutorial - Django Models: Creating A Model

The document discusses Django models. It covers creating a model class that subclasses django.db.models.Model, registering models in the Django admin site, using Meta options like verbose_name_plural, creating model instances, implementing __str__() and get_absolute_url() methods, using the @property decorator, and different types of Django model fields like CharField and EmailField. The goal of models is to structure and manage data in the database.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

2.

Python Django Tutorial | Django Models

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:

1. Your virtual environment is activated


2. You have registered your app in 'INSTALLED_APP' in "settings.py". 
3. You have created a Super User (admin) - python manage.py createsuperuser

Creating a model.
In your first_app/models.py

from django.db import models

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)

    gender = models.CharField(max_length=1, choices= GENDER)

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

(env) PS E:\STUDY\Python Bloging Materials\Django\project\my_project> python manage.py makemigrations


Migrations for 'first_app':
  first_app\migrations\0001_initial.py
    - Create model Person
(env) PS E:\STUDY\Python Bloging Materials\Django\project\my_project> python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, first_app, sessions
Running migrations:
  Applying first_app.0001_initial... OK

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'.

You will see django's default admin panel.

Registering Model in Django Admin Site.


There is no app named 'first_app' & no model. We will have to register this model in admn site. Go to
your app's 'admin.py' file. and add the following codes.

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:

        verbose_name_plural = 'All Persons'

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. 

Creating Objects of Models


As our model is a “Python Class” we can create as many object of this class as we want. Let’s  create
some objects.
 

__str__ Method in Django Models.


We have created three objects of  ‘Person’ class. But how they are representing in admin panel is not
readable. Here we can use "__str__" method for string representation. We will override the default
__str__ method of ‘models.Model’ class in our ‘Person’ class. 

    #Method
    def __str__(self):
        return self.first_name + self.last_name

Now see output in admin panel. 

You can also return f-string…


    def __str__(self):
        return f'{self.first_name} {self.last_name}'

get_absolute_url() Method in Django model.


Till now we were working with model. Now we will do something with the instances of our model. If you
need data about a single instance of our 'Person' class then you will have to fetch the data according to it's
primary key or a unique key. Here, our primary key is 'id'. which is not shown in 'Person' class attributes
but will be generated automatically. You can see it in your first_app/migrations folder. 
So, to call the object a with its id we will have to define a function named 'get_absolute_url()'. 

    #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.

Overall our model will look like this:

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:

Django Model Fields Data Type Description


models.BinaryField() Binary Stores binary data (e.g. images, audio or other
multimedia objects)
models.BooleanField() Boolean Stores True/False (or 0/1) values

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.TextField() Text Used to store text.


models.EmailField() Text Uses Django EmailValidator to determine what is and
(Specialized) isn't a valid.
models.FileField() Text Used for handling files (e.g. opening/closing file, upload
(Specialized) location,etc). 

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.

models.URLField() Text Works just like CharField defaulting to a 'max_length'


(Specialized) of 200 characters and checks if the string is a valid URL

You might also like