Django tutorial for beginners [step by step] part 4

django
Published: 2 weeks, 1 day ago   98


Fermesk Rashidi

Tutorial goals: Create a model, migrate it, databases in Django, create a superuser admin, register model.

Welcome to tutorial part 4. In this tutorial we are going to teach you how to create a Django model, admin interface, put some data into the model and make migrations. Alright, let's do this. :) 

Django model 

A model in Django is a class object that is stored in the database and is written in python language. An object is a collection of properties and actions. If you are making a music store website, there are definitely various data like users' data(username, emails) product detail (song title, artist, producer, year of release..., etc)  that you need to store. Django models are used for store data in the database. In this case, an artist is an object that has its own properties like name, country, genre, release year. So basically creating a model is describing real things in code with properties (which are called object properties) and actions (called methods).

Create a Django model 

We want to create a model for the artist which has some attributes like genre, country, birth year..... For this open models.py module in the music app folder and write these codes:

class Artist (models.Model):
    name = models.CharField(max_length = 250)
    country = models.CharField(max_length = 150)
    birth_year = models.IntegerField()
    genre = models.CharField(max_length = 150)

Class Artist (models.Model): In this line, we’re defining our object, or in other words our model. The special keyword defines a model is called class. Artist is the name we’re giving to our model. It should always start with a capital letter and it can not be a special reserved keyword. You can name it anything you want but if it's better to be meaningful and something easy to remember. The model lets Django know that "Artist" is a Model, so Django saves it in the database.

models.CharField: It is the way you define a text field with a limited number of characters. If the song title is more 250 characters you may get an error(if you define a function relatably in views which will be discussed in detail later).

models.TextField: It is the defining type of text field with an unlimited length of characters.

Models.IntegerField: It's an integer number in Python code by an int instance. This field is generally used to store integer numbers in the database.

Databases in Django 

A database is a repository of information stored in a well-organized way. The purpose of the database is to hold a large amount of data and allow a fast method to search for specific information and to retrieve it rapidly. If you've decided to be a backend developer it's important to know how to interact with the database and how a database works. For example, as your music store website gets bigger, you need to deal with the high amount of data like users data, song, and artist data..., etc. Django by default install SQLite which means it doesn't need to install any packages or do configurations as well. Django automatically creates an SQLite database for your project. But it's not a good option for the big project.

Django officially supports the following databases:

  • MySQL
  • MariaDB
  • SQLite
  • PostgresSQL
  • Oracle

So If your project is dealing with a high quantity of data you must install a high scale database like MySQL or oracle..., etc. A database setup by itself can be a time-consuming process with various configurations in settings.py and installing packages. Doprax supports Mysql and PostgreSQL for the Django project with all configurations and settings. If you open the settings.py file of a Django project you'll notice the DATABASES the variable has a default Python dictionary.

Migrations

The next step here is to add our model to the database. Writing models in Python have some advantages and ease our work a lot. But there’s a downside to this which is the asynchronous relationship between database and models. However, Django’s migration tackles this problem very well by ensuring that the model definition and the database schema are in sync. Also, every change that we make in our model, like adding a new field, should apply to the database too. Migrations take care of these too.

So first, we have to make a migration file using Django to apply it to our database. By doing this, Django will find out the applied changes in our model too. So you need to go to shell in the dashboard in the project section click on "open shell" button.

every change that we make in our model, like adding a new field, should apply to the database too. Migrations take care of these too. So first, we have to make a migration file using Django to apply it to our database. By doing this, Django will find out the applied changes in our model too. So you need to go to the shell in the dashboard in the project section click on "open development shell" button and type this command:

python manage.py makemigrations

Now it's time to applying the changes to the database.

python manage.py migrate

This command creates our migration file. This file is an instruction for Django on how to create tables in your database with regard to your models in the app. If you check out the music app directory you can see that a migration folder has been created which contains 2 python files:

migrations files

migrations files

Admin interface

Almost any website has an admin section. Django admin interface is one of the best features of Django. It provides a ready-to-use user interface for administrative activities. No doubt that the admin interface is important for a web project. When you create a Django project, Django automatically generates admin UI based on your project models. It is a helpful tool in adding, removing, or editing models. You can easily do manage your website with the Django admin page and also customize it for your project if necessary. 

Create Django admin interface

When you create a Django project,  Django automatically created and configured the default admin site for you. All you need to do now is create an admin user (superuser) to log in. To create an admin user, Open development shell , activate virtual envirement and run the following command afterward:

python manage.py createsuperuser

Then you will be asked to enter your desired user name:

Username: admin

Enter email address:

Email address: admin@example.com

And finally is to enter your password. Enter your password twice, the second time to confirm your password:

Password: **********
Password (again): *********
Superuser created successfully.

Now you can log in to your Django admin by clicking on the URL in the editor and adding /admin at the end of the link:

editor login page

Django admin login page

By entering the user name and password you chose. once you will log in you can see this page:

Django admin index page

Django admin index page

Register Django model in admin

In order for Django models to show up in the admin interface, you need to register them. open the admin.py in the app that you've created the models in and type this code:

from .models import Artist

admin.site.register(Artist)

Now if you log into the admin interface you can see the music section and model of artist.

registered model in admin

registered model in admin

This is the end of part 4. I'll see you in the next part. Thank you for reading. :)

Django tutorial for beginners [step by step] part 3
Django tutorial for beginners [step by step] part 5
doprax

Simple Host for Django

  • Simple interface
  • Free SSL
  • Custom Domain
  • Import from github
  • and more
Get started now

Related