API Documentation

The API of djeneralize is broken down into five modules:

djeneralize

djeneralize.PATH_SEPERATOR = '/'

The seperator used to divide hierarchy in specializations

fields

class djeneralize.fields.SpecializedForeignKey(to, to_field=None, rel_class=<class 'django.db.models.fields.related.ManyToOneRel'>, db_constraint=True, **kwargs)

Foreign key field that return the most specialized model instance of the related object.

manager

class djeneralize.manager.SpecializationManager

Customized manager to ensure that any QuerySet that is used always returns specialized model instances rather than generalized model instances.

The manager can either return final_specializations, i.e. the most specialized specialization, or the direct specialization of the general model.

contribute_to_class(model, name)

Specialization managers contribute to the model in a different way, so the code overrides what Django normally does.

direct()

Set the _final_specialization attribute on a clone of the queryset to ensure only directly descended specializations are considered.

Returns:The cloned queryset
Return type:djeneralize.query.SpecializedQuerySet
final()

Set the _final_specialization attribute on a clone of the queryset to ensure only terminal specializations are considered.

Returns:The cloned queryset
Return type:djeneralize.query.SpecializedQuerySet
get_queryset()

Instead of returning a QuerySet, use SpecializedQuerySet instead

Returns:A specialized queryset
Return type:djeneralize.query.SpecializedQuerySet

models

class djeneralize.models.BaseGeneralizationMeta

The metaclass for BaseGeneralizedModel

class djeneralize.models.BaseGeneralizationModel(*args, **kwargs)

Base model from which all Generalized and Specialized models inherit

get_as_specialization(final_specialization=True)

Get the specialized model instance which corresponds to the general case.

Parameters:final_specialization (bool) – Whether the specialization returned is the most specialized specialization or whether the direct specialization is used
Returns:The specialized model corresponding to the current model
specialization_type = None

Field to store the specialization

djeneralize.models.ensure_specialization_manager(sender, **kwargs)

Ensure that a BaseGeneralizationModel subclass contains a default specialization manager and sets the _default_specialization_manager attribute on the class.

Parameters:sender (BaseGeneralizationModel) – The new specialized model

query

class djeneralize.query.SpecializedQuerySet(*args, **kwargs)

A wrapper around QuerySet to ensure specialized models are returned.

direct()

Set the _final_specialization attribute on a clone of this queryset to ensure only directly descended specializations are considered.

Returns:The cloned queryset
Return type:SpecializedQuerySet
final()

Set the _final_specialization attribute on a clone of this queryset to ensure only terminal specializations are considered.

Returns:The cloned queryset
Return type:SpecializedQuerySet
get(*args, **kwargs)

Override get to ensure a specialized model instance is returned.

Returns:A specialized model instance
iterator()

Override the iteration to ensure what’s returned are Specialized Model instances.

utils

Utilities for djeneralize

djeneralize.utils.find_next_path_down(current_path, path_to_reduce, separator)

Manipulate path_to_reduce so that it only contains one more level of detail than current_path.

Parameters:
  • current_path (basestring) – The path used to determine the current level
  • path_to_reduce (basestring) – The path to find the next level down
  • separator (basestring) – The string used to separate the parts of path
Returns:

The path one level deeper than that of current_path

Return type:

unicode

djeneralize.utils.get_specialization_or_404(klass, *args, **kwargs)

Uses get() to return an specializaed object, or raises a Http404 exception if the object does not exist.

klass may be a BaseGeneralizedModel, SpecializationManager, or SpecializedQuerySet object. All other passed arguments and keyword arguments are used in the get() query.

Note

Like with get(), an MultipleObjectsReturned will be raised if more than one object is found.