Django Tutorial
About Lesson

In this Django Tutorial we are going to talk about Django Models, we will have a simple introduction to Django Models and after that we are going to create a practical example in django models.

 

 

What are Django Models ?

According to Django Documentation a model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table, also each models is a Python class that extends from django.db.models.Model. for example if you want to create a database table for Student, than you can just create regular python class at name of Student that extends from django.db.models.Model.

 

 

Now open your mysite project, after that go to your polls app that we have already created, and after that open models.py file, so this is the file that you can create your models in their, and this is our table or database model.

 

mysite/polls/models.py

 

You can see that we have Question model, it means that we want to create database table at name of Question, and there are two fields for the table, basically there are three fields, the id field is auto created and it will be created automatically by django, also there are different field types that you can use in Django Tutorial, for right now we have used CharField(), it is a string field, for small- to large-sized strings. also we have used DateTimeField(), it is a a date and time, represented in Python by a datetime.datetime instance. Takes the same extra arguments as DateField.

 

Also make sure that you have already added your django polls in the settings.py INSTALLED_APPS, because if you don’t do this there will be no changes in the migrations time.

 

 

 

Now after creating of the models, you need to do migrations and also migrate your django project.

 

 

This will be the result in the terminal.

 

 

Now you need to migrate your project, using migrate you can add your table with fields in the sqlite3 database.

 

 

This is the result for the migrate.

 

 

 

Now you are done, you have created a table name at name of Question with three fields, if you check your sqlite database in the SQLite Studio, you will see your table name like this.

Django Tutorial - Django Models
Django Tutorial – Django Models