Model in Django
To create Django Models, one needs to have a project and an app working in it. After you start an app you can create models in app/models.py. Before starting to use a model let’s check how to start a project and create an app named geeks.py
Refer to the following articles to check how to create a project and an app in Django.
Create Model in Django
Syntax
from django.db import models
class ModelName(models.Model):
field_name = models.Field(**options)
To create a model, in geeks/models.py Enter the code,
Python3
# import the standard Django Model # from built-in library from django.db import models # declare a new model with a name "GeeksModel" class GeeksModel(models.Model): # fields of the model title = models.CharField(max_length = 200 ) description = models.TextField() last_modified = models.DateTimeField(auto_now_add = True ) img = models.ImageField(upload_to = & quot images / " ) # renames the instances of the model # with their title name def __str__( self ): return self .title |
This code defines a new Django model called “GeeksModel” which has four fields: “title” (a character field with a maximum length of 200), “description” (a text field), “last_modified” (a date and time field that automatically sets the date and time of creation), and “img” (an image field that will be uploaded to a directory called “images”). The __str__ method is also defined to return the title of the instance of the model when the model is printed.
This code does not produce any output. It is defining a model class which can be used to create database tables and store data in Django.
Whenever we create a Model, Delete a Model, or update anything in any of models.py of our project. We need to run two commands makemigrations and migrate. makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created app’s model which you add in installed apps whereas migrate executes those SQL commands in the database file.
So when we run,
Python manage.py makemigrations
SQL Query to create above Model as a Table is created and
Python manage.py migrate
creates the table in the database.
Now we have created a model we can perform various operations such as creating a Row for the table or in terms of Django Creating an instance of Model. To know more visit – Django Basic App Model – Makemigrations and Migrate
Render a Model in Django Admin Interface
To render a model in Django admin, we need to modify app/admin.py. Go to admin.py in geeks app and enter the following code. Import the corresponding model from models.py and register it to the admin interface.
Python3
from django.contrib import admin # Register your models here. from .models import GeeksModel admin.site.register(GeeksModel) |
This code imports the admin module from the django.contrib package and the GeeksModel class from the models module in the current directory. It then registers the GeeksModel class with the Django admin site, which allows the model to be managed through the Django admin interface. This means you can perform CRUD(Create, Read, Update, Delete) operations on the GeeksModel using the Django admin interface.
This code does not produce any output, it simply registers the GeeksModel with the admin site, so that it can be managed via the Django admin interface.
Now we can check whether the model has been rendered in Django Admin. Django Admin Interface can be used to graphically implement CRUD (Create, Retrieve, Update, Delete).
To check more on rendering models in django admin, visit – Render Model in Django Admin Interface
Django Models
A Django model is the built-in feature that Django uses to create tables, their fields, and various constraints. In short, Django Models is the SQL Database one uses with Django. SQL (Structured Query Language) is complex and involves a lot of different queries for creating, deleting, updating, or any other stuff related to a database. Django models simplify the tasks and organize tables into models. Generally, each model maps to a single database table.