Skip to main content

Taking the O out of ORM.

Project description

https://img.shields.io/pypi/v/django-model-values.svg https://img.shields.io/pypi/pyversions/django-model-values.svg https://img.shields.io/pypi/status/django-model-values.svg https://img.shields.io/travis/coady/django-model-values.svg https://img.shields.io/codecov/c/github/coady/django-model-values.svg https://readthedocs.org/projects/django-model-values/badge

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.8

Tests

100% branch coverage.

$ pytest [--cov]

Changes

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

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-model-values-0.5.1.tar.gz
Algorithm Hash digest
SHA256 78b736bbc0dfea7e3bd03cb52770f93a993df2de5ed65339afd1e3f9220d6bc0
MD5 7963559f8242b8391edf9ed4ddd49cd8
BLAKE2b-256 12f1c4c0a62f0f88e0907198fe18af3a0a29f1dae9d871db26ca9edb76181b59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_model_values-0.5.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7674de4ce4ab0f3fc5faf92f4b9d975bbf9fca2eba1725d4ce8f5bff1046ae8e
MD5 1f300b6f314d7cedd17ffd625cada64e
BLAKE2b-256 003fefe20970cf5fe2286f2a5ab4912189e3291ea91e211011723b09b548a1bc

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