Skip to main content

Taking the O out of ORM.

Project description

image 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]

Changes

1.2

  • Python >=3.6 required
  • Django >=2.2 required

1.1

  • Django 3 support

1.0

  • Update related methods moved with deprecation warnings
  • Extensible change detection and updates
  • Django 2.2 functions

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 operators any and all
  • 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


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.2.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django-model-values-1.2.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.1

File hashes

Hashes for django-model-values-1.2.tar.gz
Algorithm Hash digest
SHA256 ad36f3b55c86ab27d5d6abe0cca785461dff52c1e68fa67a126b782dfdf9da61
MD5 435c2103cd0eafe57c975a4140981c02
BLAKE2b-256 3051ae3e8e0b94aed51ab8c18b55b7b44c3dea4aaadae521af5014dbefc00bbe

See more details on using hashes here.

File details

Details for the file django_model_values-1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_model_values-1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.1

File hashes

Hashes for django_model_values-1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8ffe981aca633007993bcfff38eaeacc18cd13e0ac508def920c493d56b46f1c
MD5 b74a8c1242e64dd5af1a5bc048f6b77d
BLAKE2b-256 aec94d25244e229022bcc49f0a1e5ae5808d1b0cf4618433d4ed3f94fcd9d4f7

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