Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Numpy
    Tensorflow
    Interview Questions & Answers

    Django ORM

    Django ORM has been mentioned in all of the database & model tutorials. It is an amazing implementation to increase development speed.The other important topic is of Querysets. These are an important part of ORMs in general. We have used Querysets many times in our tutorials.

    What is an ORM?

    ORM is an acronym for the object-relational mapper. The ORM’s main goal is to transmit data between a relational database and application model. The ORM automates this transmission, such that the developer need not write any SQL.

    ORM, as from the name, maps objects attributes to respective table fields. It can also retrieve data in that manner.

    Django is shipped with its own ORM. It is a very efficient ORM and is tightly coupled with the Django framework. Django ORM is suitable for handling low-medium complexity queries. Although, some consider SQLAlchemy ORM to be a better option. The migrations feature by Django is also a part of Django ORM.

    Django ORM vs SQLAlchemy?

    In most cases, people prefer SQLAlchemy over Django ORM. The reason being SQLAlchemy is more adept to work with high-complexity data structures. Django ORM is powerful in its own way. The ORM provides features integrated with Django which are more important than just performance improvement.

    Currently, Django ORM is not swappable with SQLAlchemy. There are some hacks to do it but it’s unofficial. Django’s future releases are expected to provide this functionality. Also, both SQLAlchemy and Django ORM is actively developed.

    Querysets in Django

    We all use queries to retrieve data from the database. Querysets are Django’s way to retrieve data from the database. The Django ORM lets us use Querysets.

    A Queryset is a list of objects of a model. We use Querysets to filter and arrange our data. These make our work as a Python developer easier.

    Different Relationships between Fields

    Django ORM provides a level of abstraction which makes it easy to work with objects. ORM will automatically relate the object’s attributes to corresponding table fields. We are going to make some models and check their field relationships in other tables.

    It is expected that you can make models and applications.

    One to One Relationship

    A one-to-one relationship exists between two tables. For each row in table1, there shall be a row/ entity in table2.

    There are two tables here, Customer and Vehicle. Every customer owns only one vehicle. Thus, a one-to-one relationship exists. Now, paste this code in models.py to implement this.

    Code:

    from django.db import models
    #Ducatindia #DjangoTutorials
    # Create your models here.
    class Customer(models.Model):
    name = models.CharField(max_length=255)
    class Vehicle(models.Model):
    name = models.CharField(max_length=255)
    customer = models.OneToOneField(
    Customer,
    on_delete=models.CASCADE,
    related_name='vehicle'
    )
    

    One to Many Relationships

    Just like one to one we can have one-to-many relationships. We are going to use a different model for showcasing this.

    A one to many relationships is where one object from table1 can have multiple relations with entities in table2. Although, table2 objects will have only one relation to the object of table1.

    The code for this model is:

    from django.db import models

    #Ducatindia #DjangoTutorials
    # Create your models here.
    class Customer(models.Model):
    name = models.CharField(max_length=255)
    class Vehicle(models.Model):
    name = models.CharField(max_length=255)
    customer = models.ForeignKey(
    Customer,
    on_delete=models.CASCADE,
    related_name='Vehicle'
    )
    

    Many to Many Relationships

    We can also implement many to many relations in the database. In this case, we are taking examples of multiple drivers.

    To implement the same in model, paste this code in models.py:

    from django.db import models
    # Create your models here.
    #Ducatindia #Many to Many Relationship
    class Worker(models.Model):
    name = models.CharField(max_length=255)
    class Machine(models.Model):
    name = models.CharField(max_length=255)
    worker = models.ManyToManyField(
    Worker,
    related_name='Machine'
    )
    


     

     

    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.