Skip to main content

Django model mixins and utilities

Project description

Django model mixins and utilities.

models.InheritanceCastModel

This abstract base class can be inherited by the root (parent) model in a model-inheritance tree. It allows each model in the tree to “know” what type it is (via an automatically-set foreign key to ContentType), allowing for automatic casting of a parent instance to its proper leaf (child) type.

For instance, if you have a Place model with subclasses Restaurant and Bar, you may want to query all Places:

nearby_places = Place.objects.filter(location='here')

But when you iterate over nearby_places, you’ll get only Place instances back, even for objects that are “really” Restaurant or Bar. If you have Place inherit from InheritanceCastModel, you can just call the cast() method on each Place and it will return an instance of the proper subtype, Restaurant or Bar:

from model_utils.models import InheritanceCastModel

class Place(InheritanceCastModel):
    ...

class Restaurant(Place):
    ...

nearby_places = Place.objects.filter(location='here')
for place in nearby_places:
    restaurant_or_bar = place.cast()
    ...

models.TimeStampedModel

This abstract base class just provides self-updating created and modified fields on any model that inherits it.

managers.QueryManager

Many custom model managers do nothing more than return a QuerySet that is filtered in some way. QueryManager allows you to express this pattern with a minimum of boilerplate:

from django.db import models
from model_utils.managers import QueryManager

class Post(models.Model):
    ...
    published = models.BooleanField()
    pub_date = models.DateField()
    ...

    objects = models.Manager()
    public = QueryManager(published=True).order_by('-pub_date')

The kwargs passed to QueryManager will be passed as-is to the QuerySet.filter() method. You can also pass a Q object to QueryManager to express more complex conditions. Note that you can set the ordering of the QuerySet returned by the QueryManager by chaining a call to .order_by() on the QueryManager (this is not required).

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-utils-0.3.0.tar.gz (5.6 kB view details)

Uploaded Source

File details

Details for the file django-model-utils-0.3.0.tar.gz.

File metadata

File hashes

Hashes for django-model-utils-0.3.0.tar.gz
Algorithm Hash digest
SHA256 52a28406ccb31512fcf206a6371c76f65e2dbace757c298d014d24ec7eb18dcc
MD5 88f3e58ce5f06d3fe4b60b675ba5d571
BLAKE2b-256 38b092e8cbf9d9b2bd0fd6d08eb2abca37603f589d55ea62b8f9342279ec723f

See more details on using hashes here.

Provenance

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