Django model utilities for encouraging direct data access instead of unnecessary object overhead. Implemented through compatible method and operator extensions to
The goal is to provide elegant syntactic support for best practices in using Django's ORM. Specifically avoiding the inefficiencies and race conditions associated with always using objects.
Typical model usage is verbose, inefficient, and incorrect.
book = Book.objects.get(pk=pk) book.rating = 5.0 book.save()
The correct method is generally supported, but arguably less readable.
model_values encourages the better approach with operator support.
Book.objects[pk]['rating'] = 5.0
Similarly for queries:
(book.rating for book in books) books.values_list('rating', flat=True) books['rating']
Column-oriented syntax is common in panel data layers, and the greater expressiveness cascades.
QuerySets also support aggregation and conditionals.
books.values_list('rating', flat=True).filter(rating__gt=0) books['rating'] > 0 books.aggregate(models.Avg('rating'))['rating__avg'] books['rating'].mean()
Managers provide a variety of efficient primary key based utilities. To enable, instantiate the
Manager in your models. As with any custom
Manager, it doesn't have to be named
objects, but it is designed to be a 100% compatible replacement.
from model_values import Manager class Book(models.Model): ... objects = Manager()
F expressions are also enhanced, and can be used directly without model changes.
from model_values import F .filter(rating__gt=0, last_modified__range=(start, end)) .filter(F.rating > 0, F.last_modified.range(start, end))
% pip install django-model-values
100% branch coverage.
% pytest [--cov]
- Python >=3.8 required
- Django >=3.2 required
- Python >=3.7 required
- Django 4 support
- Django 3.2 support
- Python >=3.6 required
- Django >=2.2 required
- Django 3 support
- Update related methods moved with deprecation warnings
- Extensible change detection and updates
- Django 2.2 functions
- Transform functions
- Named tuples
- Window functions
- Distance lookups
- Django 2.1 functions
- Expressions in column selection
- Spatial lookups and functions
- Django 2.0 support
- Django 1.9 database functions
bulk_updatesupports additional fields
- Lookup methods and operators
Fexpressions and aggregation methods
- Database functions
- Conditional expressions for updates and annotations
- Bulk updates and change detection
- Change detection
- Groupby functionality
- Named tuples