Skip to content
Back to top
View this page
Edit this page

# polymorphic.managers

The manager class for use in the models.

## The `PolymorphicManager` class

class polymorphic.managers.PolymorphicManager

  Bases: `Manager`

  Manager for PolymorphicModel

  Usually not explicitly needed, except if a custom manager or a custom queryset class is to be used.

  queryset_class

    alias of `PolymorphicQuerySet`

  create_from_super(obj, **kwargs)

    Create an instance of this manager’s model class from the given instance of a parent class.

    This is useful when “promoting” an instance down the inheritance chain.

    Parameters:

      - obj – An instance of a parent class of the manager’s model class.

      - kwargs – Additional fields to set on the new instance.

    Returns:

      The newly created instance.

  get_queryset()

    Return a new QuerySet object. Subclasses can override this method to customize the behavior of the Manager.

## The `PolymorphicQuerySet` class

class polymorphic.managers.PolymorphicQuerySet

  Bases: `QuerySet`

  QuerySet for PolymorphicModel

  Contains the core functionality for PolymorphicModel

  Usually not explicitly needed, except if a custom queryset class is to be used.

  classmethod as_manager()

    Override base `as_manager()` to return a manager extended from `polymorphic.managers.PolymorphicManager`.

  __init__(*args, **kwargs)
  aggregate(*args, **kwargs)

    translate the polymorphic field paths in the kwargs, then call vanilla aggregate. We need no polymorphic object retrieval for aggregate => switch it off.

  annotate(*args, **kwargs)

    translate the polymorphic field paths in the kwargs, then call vanilla annotate. _get_real_instances will do the rest of the job after executing the query.

  bulk_create(objs, batch_size=None, ignore_conflicts=False)

    Insert each of the instances into the database. Do not call save() on each of the instances, do not send any pre/post_save signals, and do not set the primary key attribute if it is an autoincrement field (except if features.can_return_rows_from_bulk_insert=True). Multi-table models are not supported.

  defer(*fields)

    Translate the field paths in the args, then call vanilla defer.

    Also retain a copy of the original fields passed, which we’ll need when we’re retrieving the real instance (since we’ll need to translate them again, as the model will have changed).

  delete()

    Deletion will be done non-polymorphically because Django’s multi-table deletion mechanism is already walking the class hierarchy and producing a correct deletion graph. Introducing polymorphic querysets into the deletion process disrupts the model hierarchy/relationship traversal.

  get_real_instances(base_result_objects=None)

    Cast a list of objects to their actual classes.

    This does roughly the same as:

        return [ o.get_real_instance() for o in base_result_objects ]

    but more efficiently.

    Return type:

      PolymorphicQuerySet

  instance_of(*args)

    Filter the queryset to only include the classes in args (and their subclasses).

  non_polymorphic()

    switch off polymorphic behaviour for this query. When the queryset is evaluated, only objects of the type of the base class used for this query are returned.

  not_instance_of(*args)

    Filter the queryset to exclude the classes in args (and their subclasses).

  only(*fields)

    Translate the field paths in the args, then call vanilla only.

    Also retain a copy of the original fields passed, which we’ll need when we’re retrieving the real instance (since we’ll need to translate them again, as the model will have changed).

  order_by(*field_names)

    translate the field paths in the args, then call vanilla order_by.