Introduction
Today, I will take a new part of Python Django from my tutorial series. In this part, you will learn the Database model features.
I already told you the following:
- Installation Of Django: Python Framework
- Python Django Tutorial: Migration Of Database Models - Part One
Store data in Database:
- Firstly, follow my article.
- After reading this article now we move on to some extra features of models in Django that will help for customization of the database.
- So, I have already made a demo of a Django project that has the main Django app.
- Open models.py file in the Django app folder.
This file has code as below:

- Now we want to first create a database using migration.
- Open Command Prompt with the project root directory.
- Enter Commands
- python manage.py makemigrations <Enter Django app name>
- python manage.py sqlmigrate <Enter django app name>
- <prefix of the file which generates from the above command like 0001>
- python manage.py migrate
- So you have to create a database table successfully then we want to save data through command prompt in the database.
- Now Enter command in CMD: python mange.py shell. The above command is used for opening a shell of a demo project that gives direct access to project class and models that are shown as below image.

- Now I have class named UserData that is shown in code that also defines the models or Table name.
- Now we store the data in the database using some code as below.
- from main.models import UserData
- user= UserData(emailid="[email protected]",password="12345678")
- user.save()

Here main.models denote the file in the main folder that has UserData models. So we access those models using the above code. The second code stored data in is user object. save() is a predefined function that saves object value into the database.
the user object is given many features to see data of objects which were saved using it.







Sonu ChaudharyPosted May 16, 2016, 3:34 PM
good one...