Instagram
youtube
Facebook
  • 2 years, 2 months ago
  • 859 Views

Everything about Django Models and ORM

Mradul Mishra
Table of Contents

In this blog we are going to learn about the Django Models and ORM.

Table of Content

  1. Introduction to Django Models.
  2. Advantages of Using Django Models.
  3. Supported Databases
  4. How to build first Django Models.
  5. Relationships in Django
  6. Types of Fields.
  7. Types of Queries in Django ORM.

 

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: