Skip to main content

Django model mixins and utilities

Project description

Django model mixins and utilities.

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

TimeStampedModel

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

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

Uploaded Source

File details

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

File metadata

File hashes

Hashes for django-model-utils-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dfa57fcfb7796e52991538c6a5892fdccd8db066d2a073461d869cc931971af1
MD5 61c340ae8140cb076b0b816c197a9404
BLAKE2b-256 cde0ab4ff8bc2abc739544223f50dd51ebb19c7e435ade7d385a9827d500c322

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