polymorphic.models

Seamless Polymorphic Inheritance for Django Models

class polymorphic.models.PolymorphicModel(*args, **kwargs)

Bases: django.db.models.base.Model

Abstract base class that provides polymorphic behaviour for any model directly or indirectly derived from it.

PolymorphicModel declares one field for internal use (polymorphic_ctype) and provides a polymorphic manager as the default manager (and as ‘objects’).

get_real_instance()

Upcast an object to it’s actual type.

If a non-polymorphic manager (like base_objects) has been used to retrieve objects, then the complete object with it’s real class/type and all fields may be retrieved with this method.

Note

Each method call executes one db query (if necessary). Use the get_real_instances() to upcast a complete list in a single efficient query.

get_real_instance_class()

Return the actual model type of the object.

If a non-polymorphic manager (like base_objects) has been used to retrieve objects, then the real class/type of these objects may be determined using this method.

pre_save_polymorphic(using='default')

Make sure the polymorphic_ctype value is correctly set on this model.

save(*args, **kwargs)

Calls pre_save_polymorphic() and saves the model.

polymorphic_ctype

The model field that stores the ContentType reference to the actual class.