Learn HTML
Learn CSS
Learn Javascript
Django Introduction
Django Project & File Structure
Django Create & Structure Directory of Application
Django View
Django Template
Django Dynamic Templates Using DLT
Django if,elif,else & for loop tags
Django Static Files
Django Template & Static File Inheritance
Django Hyperlinks
django Get Post Csrf
Django Administration
Django Model class
Django Form
Django Get Form Data & Data Validation
Django Redirect Page After Submit
Django Save,Update & Delete Form in database
Django Dynamic URL
Django User Authentication System
It is the definitive source of information about the data. It contains the essential fields and behaviors of the stored data. Each model maps to a single table in a database.
Model class represent a table in a data base. Each or single attributes of model represents a single database
filed.
It means one attribute = one field.
The default database of django is sqlite.
To create model class, models.py file will used.You will get this file inside application folder. You will
create your model class inside this file.
Syntax of creating model class:
Here we created two fields but you can create how many you want.
Here in the bracket of class, you have to write models.Model. You can't change it.
Field name will be the column of database table. It means, here you are creating column for a database
table.
FieldType is the type of field name or column.
Example: email,password etc.
Here in the class, we created four fields studentId, studentName, studentEmail and studentEmailPassword. It
means that we created a table in the database and in the table we have columns like student id, name, email
and password.
We know that student id is numerical integer data like 1, 1001, etc. That why we pass the field type
IntegerField. By doing this user will only able to input integer number in this field. Similarly in student
name can be only characters. That's why we used CharFiled. Here we also pass an arguments max_length=70. It
means that the length of name must be less than equal 70. User can't put a name which contain more than 70
characters.
Look in the previous example, we didn't make any column as primary key but it is necessary. But if you don't
do then automatically a new column will be created which name will be id, fieldtype will be integer with auto
increment. Auto increment means the value of this column cell will be automatically increase by one(by
default) in the next cell.
After creating a model, to use that model means creating a table in the database according to the model follow the steps:
By using these command, you will create a table in the database according to model.
Suppose we make changes in the model. Then you have to run these two commands again to see the changes in
database. It means you have to run these two commands each time you make changes in you model. If you don't
make any changes in your model then you don't need to run these two commands.
Previously we created a database table. But if look in the admin panel then you will see there is not tables
named Student. To get the table in admin panel, you have to register the model class in django admin.
Steps to register model class in admin:
Now if you go to the admin panel then you will see the table.
Now if you go inside the table, you will see the table is empty. But you will see add option on the left top side.
If you click on that option then a dialog box will open. There fill the information and click on save. By doing this you are adding a row means a student info in the table.
After adding, if you go inside of the table then you will see the records like this:
If you click on the record or row then you will see options for edit the record or row and also can save it.
You will also see options for delete the record or row.
To do this we have to create model admin class.
We have to create admin model class in admin.py files present in the application file.
Now let's go to admin panel and see. Now if you want to edit or delete the records then click on each row primary key value cell.
all():It returns a copy of current queryset. If you want to get the data of whole table at once then you will use this method or function.
get():If you want to get a single data from the database table then use this function of method. To get single value you will use primary key column.
Suppose in your table id is the primary key column and you want to get id number 4 data.
In this case write:
Step 1:
Import your own model class in views.py file from models.py file.
Here is our thirdApplication name and Student is the class name.
Step 2:
Create view function.You will create this view function in that application where you wrote code for create
model.
Step 3:
Then we have to define url for this view function.We have learn two methods for define url.You can follow any
one.
Here Student is the class or model name. In context parameter, we will pass value as a dictionary. Now we will
use the key value of context parameter in the template file.