Prerequisites : Django Creating apps
Models -
According to
Django Models, 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.
The basics:
- Each model is a Python class that subclasses django.db.models.Model.
- Each attribute of the model represents a database field.
- With all of this, Django gives you an automatically-generated database-access API.
Django models are used as a structure to define fields and their types which will be saved in the database. Whatever changes we want to make in the database and want to store them in the database permanently are done using Django Models. A table for a phone in the database can be imagined as:
Create table in database using Django -
We need to create a new app named
product so that we can define all properties of phone described in the above-provided image. Open your terminal and run following command:
python manage.py startapp product
Now, our directory will be:

After creating the app, don't forget to mention it in
geeks_site/settings.py
under
INSTALLED_APPS.

Also, register it with admin by adding following line of code to
product/admin.py. By registering it with admin, you make sure that admin of the site will be aware that new table schema for database has been prepared.
Python3 1==
from django.contrib import admin
# Register your models here.
from product.models import Phone
admin.site.register(Phone)
Now, navigate to
product/models.py. You will see a file with following lines:
Python3 1==
from django.db import models
# Create your models here.
We use a python class to define models which inherit parent class named
Model defined in
django.db.models package.
Add the following lines of code to this file:
Python3 1==
from django.db import models
# Create your models here.
class Phone(models.Model):
Price = models.IntegerField()
RAM = models.IntegerField()
ROM = models.IntegerField()
Front_camera = models.IntegerField()
Rear_camera = models.IntegerField()
Battery = models.IntegerField()
Screen_size = models.DecimalField(max_digits = 2, decimal_places = 1)
Color = models.CharField(max_length = 120)
Quantity = models.IntegerField()
Similar Reads
Django Models | Set - 2 Model Fields - Model fields define the datatype which will be stored in the provided variable. To store price, the integer type is more suitable. To store height, length etc. float/decimal type is more suitable. To store title or heading, it is better to provide character limit. For writing an artic
2 min read
Django Models A Django model is a Python class that represents a database table. Models make it easy to define and work with database tables using simple Python code. Instead of writing complex SQL queries, we use Djangoâs built-in ORM (Object Relational Mapper), which allows us to interact with the database in a
8 min read
Django ModelFormSets Django ModelFormsets provide a powerful way to manage multiple model-based forms on a single page. They allow you to create, update, or delete multiple instances of a model at once, using a group of forms that correspond to the model's fields.Think of ModelFormsets as a collection of forms linked to
2 min read
TimeField - Django Models TimeField is a time field which stores time, represented in Python by a datetime.time instance. As the name suggests, this field is used to store an object of datetime created in python. The default form widget for this field is a TextInput. The admin uses two separate TextInput widgets with JavaScr
5 min read
UUIDField - Django Models UUIDField is a special field to store universally unique identifiers. It uses Python's UUID class. UUID, Universal Unique Identifier, is a python library that helps in generating random objects of 128 bits as ids. It provides the uniqueness as it generates ids on the basis of time, Computer hardware
4 min read