In this blog we are going to learn about the Django Models and ORM.
Table of Content
|
Introduction to Django Models.
-
A model in django project is a file where are the database related work is done.
-
Django models have features to create database tables, define there datatypes, and other proporties like relationships, validations, proporties.
-
Django models reduces the development time of creation and management of databases by converting database tables into python classes.
-
Django uses orm(object relational mappers) which is a pythonic way of writing sql queries.
Example of a sample Django Models
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
Advantages of Using Django Models
-
Easy to Use.
-
Converts complex sql queries into simple classes.
-
Management of Tables becomes easy as everything is arranged in the classes.
-
Making changes and updation of table becomes easy using migration functionality.
-
Has a very good support to manage relationship like One-To-One, Many-To-Many, One-To-Many. It also has features to manage Primary keys and Foriegn Keys.
Supported Databases in Django
-
PostgreSQL
-
MariaDB
-
MySQL
-
Oracle
-
SQLite
other than these, django also have support for other databases like sql server, mongo db etc. using third party libraries.
How to build first Django Models?
- To start creating your first django model you need to open models.py files inside your django application.
Add a comment: