Taking the O out of ORM.
Project description
Django model utilities for encouraging
direct data access instead of unnecessary object overhead. Implemented
through compatible method and operator extensions to QuerySets
and
Managers
.
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.
Usage
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.
Book.objects.filter(pk=pk).update(rating=5.0)
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))
Read the documentation.
Installation
$ pip install django-model-values
Dependencies
- django >=1.11
Tests
100% branch coverage.
$ pytest [--cov]
Changes
0.6
- Transform functions
- Named tuples
- Window functions
- Distance lookups
- Django 2.1 functions
EnumField
- Annotated
items
- Expressions in column selection
0.5
F
expressions operatorsany
andall
- Spatial lookups and functions
- Django 2.0 support
0.4
upsert
method- Django 1.9 database functions
bulk_update
supports additional fields
0.3
- Lookup methods and operators
F
expressions and aggregation methods- Database functions
- Conditional expressions for updates and annotations
- Bulk updates and change detection
0.2
- Change detection
- Groupby functionality
- Named tuples
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-model-values-0.6.tar.gz
.
File metadata
- Download URL: django-model-values-0.6.tar.gz
- Upload date:
- Size: 23.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7d41d70d75a999df98a29e7df9268c8da15374a9678d232efff265379ad6cd9 |
|
MD5 | 4a296b9907c6195b9b264162d34ac4c8 |
|
BLAKE2b-256 | a7c5e2375826e084e3f1f1ab90dd33f4c681b01fd5ea5d73db0b7b630a4eed42 |
File details
Details for the file django_model_values-0.6-py2.py3-none-any.whl
.
File metadata
- Download URL: django_model_values-0.6-py2.py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d66b65a0c0a33463072e14420f664adf5d3d8be39bee2d63d1b9d056bc8b7875 |
|
MD5 | 2f8daffd9cfb5b50c810bad4434890ff |
|
BLAKE2b-256 | 1d651d0bfffaeba3ca824ceccacca2f9242e64a1eb0b228191cc514fadf14e69 |