Django ORM support

Description

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.

Technology support

Technology Version Supported
Django ORM 2.0 to 6.0

What results can you expect?

Objects

Icon Description Comment
Python Django Entity Python Django Entity

Python Django Unknown Entity
Python Django Entity: 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.

Python Django Unknown Entity: created when a supported Django Manager API is used on a class that is not detected as a Django model class. This can happen when the model class is not analyzed or when the model class is not correctly detected by the analyzer, for example due to dynamic code or imports.
Python Django Entity Operation Python Django Entity Operation

Python Django Unknown Entity Operation
Python Django Entity Operation: created when one of the supported Django Manager APIs is used on a Django model class inheriting from django.db.models.Model.

Python Django Unknown Entity Operation: created when a supported Django Manager API is used on a class that is not detected as a Django model class.
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.

Code examples

Django Entity

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

Django Unknown Entity

class A():
    def __init__(self):
        self.models = dict()
    
    def f(self):
        unknown_model = self.models["foo"]
        unknown_model.objects.filter(x=1)

Will create the following objects and links:

unknown_entity_call_graph.png

Supported persistence SQL databases

Supported operations

The analyzer detects Django ORM access to database operations through a model manager, such as <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:

from django.apps import apps

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
  • aget
  • aget_or_create
  • aggregate
  • ain_bulk
  • alast
  • alatest
  • all
  • annotate
  • count
  • dates
  • datetimes
  • distinct
  • earliest
  • exclude
  • exists
  • filter
  • first
  • get
  • get_or_create
  • get_subclass
  • in_bulk
  • last
  • latest
  • select_subclasses
  • values
  • values_list
Insert
  • abulk_create
  • acreate
  • aget_or_create
  • update_or_create
  • bulk_create
  • create
  • get_or_create
  • get_queryset
  • 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

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