Django ORM support

Introduction

Django ORM is an Object-Relational Mapping framework that is part of the Django web framework. It provides a high-level interface to interact with databases using Python objects instead of SQL queries.

The analyzer detects Django ORM method calls made on model managers, creates corresponding objects and links database operations.

Examples

Model definition: countries/models.py

from django.db import models


class Country(models.Model):
    ...

CRUD operation using Django ORM:

import django
from countries.models import Country


class X:
    def __init__(self):
        self.FR = Country.objects.get(id="FR")

Will create the following objects and links:

django-ORM-entity.png

Objects

  • a Django Entity object is created when a Django model class inheriting from django.db.models.Model is found during the analysis, or when the analyzer detects supported Django Manager APIs on a Django model class
  • a Django Entity Operation object is created when one of the supported Django Manager APIs is used on a Django model class inheriting from django.db.models.Model.
Icon Description
Python Django Entity Python Django Entity
Python Django Entity Operation Python Django Entity Operation

Supported persistence SQL databases

Supported operations

The analyzer detects Django ORM access to database operations through a model manager: <Model>.objects.<API method>.

To get model classes dynamically, django.apps.apps.get_model() and django.apps.AppConfig.get_model() are supported, when the name can be resolved to a string:

app = 'blog'
article_model = 'Article'
article = apps.get_model(app, article_model)
articles = article.objects.all()
Entity Operation Supported APIs
Select
  • aaggregate
  • acount
  • aearliest
  • aexists
  • afirst
  • age
  • aget_or_create
  • aggregate
  • ain_bulk
  • alast
  • alatest
  • all
  • annotate
  • count
  • dates
  • datetimes
  • distinct
  • earliest
  • exclude
  • exists
  • filter
  • first
  • get
  • get_or_create
  • in_bulk
  • last
  • latest
  • values
  • values_list
Insert
  • abulk_create
  • acreate
  • aget_or_create
  • update_or_create
  • bulk_create
  • create
  • get_or_create
  • save
  • update_or_create
Update
  • abulk_update
  • aupdate
  • aupdate_or_create
  • bulk_update
  • save
  • save_base
  • select_for_update
  • updat
  • update_or_create
Delete
  • adelete
  • bulk_delete
  • delete
Link Type Caller type Callee type Comment
callLink
  • Python Function
  • Python Method
  • Python Module
  • Python Django Entity Operation
relyonLink
  • Python Django Entity
  • Python Class
useSelectLink
  • Python Django Entity Operation: Select
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables for Python when the object is not analyzed.
useInsertLink
  • Python Django Entity Operation: Add
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables for Python when the object is not analyzed.
useUpdateLink
  • Python Django Entity Operation: Update
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables for Python when the object is not analyzed.
useDeleteLink
  • Python Django Entity Operation: Remove
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables for Python when the object is not analyzed.

Known limitations

The following features are not supported:

  • custom managers with overridden methods
  • custom table names in custom managers: the name of the class inheriting from django.db.models.Model is used as table name
  • Q() objects for complex queries