Skip to main content

Taking the O out of ORM.

Project description

image image image image image build image CodeQL 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.7.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_model_values-1.7-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file django_model_values-1.7.tar.gz.

File metadata

  • Download URL: django_model_values-1.7.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_model_values-1.7.tar.gz
Algorithm Hash digest
SHA256 40884bd5214b0c818469af2865a26c0bcae059ea2849da1efa6294d19a5b19d8
MD5 e1438b542ec5b7a5f2fcc8fd948bbeee
BLAKE2b-256 970260f0cb9a4c818e7ed27d582bdcce773a906b32fddaae3d401a840fb6ef0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_model_values-1.7.tar.gz:

Publisher: release.yml on coady/django-model-values

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for django_model_values-1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b6e47de538c3c1c0ef33bd85566a17fc9f1ae58f7012a601fdfeedda8a210d5e
MD5 7da32fc614d795d60e0cf0bbeb9045d4
BLAKE2b-256 88d29e5afb8e9f42e096a7b2ef39d2081189a538f577f042ece7fa23311d10de

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_model_values-1.7-py3-none-any.whl:

Publisher: release.yml on coady/django-model-values

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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