Skip to main content

Taking the O out of ORM.

Project description

image image image image image image image image image image image

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))

Installation

% pip install django-model-values

Tests

100% branch coverage.

% pytest [--cov]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-model-values-1.6.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

django_model_values-1.6-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file django-model-values-1.6.tar.gz.

File metadata

  • Download URL: django-model-values-1.6.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for django-model-values-1.6.tar.gz
Algorithm Hash digest
SHA256 fde8d8d9f9b9d624e38ba8b7f906826cccc7d0a4a0953ca58931df33070e9cd0
MD5 6be525d4bfd4c62678617a34831fe918
BLAKE2b-256 4721c4013e109311a33358bd18b6024b525d5b66f800174617fafc8eb2b33f35

See more details on using hashes here.

File details

Details for the file django_model_values-1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for django_model_values-1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ba6a7454f6a7a0d4c1fdf5526919e91bdcb1c4591cf1afffd096ea5cf08bf4ca
MD5 68b87c8114eb3ff6b60f9f0ceeb50998
BLAKE2b-256 d7f09a0ad5da75fa28ccd0c4b7d00776ac06631b9a04582c73dd2a416e7a6a27

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page