django_delayed_union

class django_delayed_union.base.DelayedQuerySet(*querysets, **kwargs)[source]

A class used to work around some of the issues with Django’s built-in support for set operations with querysets (such as UNION). The primary issue is that after `.union() call is made any subsequent filtering will silently fail. This class works around that issue by maintaing all of the individual querysets and not applying an operation like .union() until it’s needed.

For example, suppose we have qs = DelayedUnionQuerySet(qs0, qs1)`, then running qs = qs.filter(id=42) will be equivalent to doing qs = DelayedUnionQuerySet(qs0.filter(id=42), qs1.filter(id=42)). Then, when we actually need to evaluate the queryset say by doing obj = qs.first(), it will return qs0.union(qs1).first() behind the scenes.

Subclasses need to implement the _apply_operation(), which performs the operation such as .union() that is being delayed.

aggregate(*args, **kwargs)

Raises NotImplementedError. Documentation for aggregate:

Returns a dictionary containing the calculations (aggregation) over the current queryset

If args is present the expression is passed as a kwarg using the Aggregate object’s default alias.

all

Returns the a new delayed queryset with all(...) having been called on each of the component querysets.: Documentation for all:

Returns a new QuerySet that is a copy of the current one. This allows a QuerySet to proxy for a model manager in some cases.

annotate(*args, **kwargs)

Returns the a new delayed queryset with annotate(...) having been called on each of the component querysets.: Documentation for annotate:

Return a query set in which the returned objects have been annotated with extra data or aggregations.

as_manager(cls)

Returns the a new delayed queryset with as_manager(...) having been called on the first component queryset, while the rest remain unchanged.

bulk_create(objs, batch_size=None)

Returns the result of calling bulk_create(...) on the first component queryset. Documentation for bulk_create:

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

complex_filter(filter_obj)

Returns the a new delayed queryset with complex_filter(...) having been called on each of the component querysets.: Documentation for complex_filter:

Returns a new QuerySet instance with filter_obj added to the filters.

filter_obj can be a Q object (or anything with an add_to_query() method) or a dictionary of keyword lookup arguments.

This exists to support framework features such as ‘limit_choices_to’, and usually it will be more natural to use other methods.

count

Returns the output of count(...) after having applied the delayed operation. Documentation for count:

Performs a SELECT COUNT() and returns the number of records as an integer.

If the QuerySet is already fully cached this simply returns the length of the cached results set to avoid multiple SELECT COUNT(*) calls.

create(**kwargs)

Returns the result of calling create(...) on the first component queryset. Documentation for create:

Creates a new object with the given kwargs, saving it to the database and returning the created object.

dates(field_name, kind, order='ASC')

Raises NotImplementedError. Documentation for dates:

Returns a list of date objects representing all available dates for the given field_name, scoped to ‘kind’.

datetimes(field_name, kind, order='ASC', tzinfo=None)

Raises NotImplementedError. Documentation for datetimes:

Returns a list of datetime objects representing all available datetimes for the given field_name, scoped to ‘kind’.

db

Return the database that will be used if this query is executed now

defer(*fields)

Returns the a new delayed queryset with defer(...) having been called on each of the component querysets.: Documentation for defer:

Defers the loading of data for certain fields until they are accessed. The set of fields to defer is added to any existing set of deferred fields. The only exception to this is if None is passed in as the only parameter, in which case all deferrals are removed (None acts as a reset option).

delete

Returns the output of delete(...) after having applied the delayed operation. Documentation for delete:

Deletes the records in the current QuerySet.

difference(*other_qs)

Raises NotImplementedError.

distinct(*field_names)

Raises NotImplementedError. Documentation for distinct:

Returns a new QuerySet instance that will select only distinct results.

earliest(field_name=None)

Returns the output of earliest(...) after having applied the delayed operation.

exclude(*args, **kwargs)

Returns the a new delayed queryset with exclude(...) having been called on each of the component querysets.: Documentation for exclude:

Returns a new QuerySet instance with NOT (args) ANDed to the existing set.

exists

Returns the output of exists(...) after having applied the delayed operation.

extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None)

Returns the a new delayed queryset with extra(...) having been called on each of the component querysets.: Documentation for extra:

Adds extra SQL fragments to the query.

filter(*args, **kwargs)

Returns the a new delayed queryset with filter(...) having been called on each of the component querysets.: Documentation for filter:

Returns a new QuerySet instance with the args ANDed to the existing set.

first

Returns the output of first(...) after having applied the delayed operation. Documentation for first:

Returns the first object of a query, returns None if no match is found.

get(*args, **kwargs)[source]

Performs the query and returns a single object matching the given keyword arguments.

Note

We cannot use PostApplyMethod for this since that does additional filtering which does not work with querysets that have been “unioned” for example.

get_or_create(defaults=None, **kwargs)

Raises NotImplementedError. Documentation for get_or_create:

Looks up an object with the given kwargs, creating one if necessary. Returns a tuple of (object, created), where created is a boolean specifying whether an object was created.

in_bulk(id_list=None)[source]

Returns a dictionary mapping each of the given IDs to the object with that ID. If id_list isn’t provided, the entire DelayedQuerySet is evaluated.

intersection(*other_qs)

Raises NotImplementedError.

iterator

Returns the output of iterator(...) after having applied the delayed operation. Documentation for iterator:

An iterator over the results from applying this QuerySet to the database.

last

Returns the output of last(...) after having applied the delayed operation. Documentation for last:

Returns the last object of a query, returns None if no match is found.

latest(field_name=None)

Returns the output of latest(...) after having applied the delayed operation.

model

Returns the model class for the DelayedQuerySet.

none

Returns the a new delayed queryset with none(...) having been called on each of the component querysets.: Documentation for none:

Returns an empty QuerySet.

only(*fields)

Returns the a new delayed queryset with only(...) having been called on each of the component querysets.: Documentation for only:

Essentially, the opposite of defer. Only the fields passed into this method and that are not already specified as deferred are loaded immediately when the queryset is evaluated.

order_by(*field_names)[source]

Returns a new DelayedQuerySet` instance with the ordering changed.

Note

We need to have a custom implementation for this because we want to change the ordering of the final queryset, not just the ordering within each component queryset.

ordered

Returns True if the DelayedQuerySet is ordered – i.e. has an order_by() clause.

Return type:bool

Returns the a new delayed queryset with prefetch_related(...) having been called on the first component queryset, while the rest remain unchanged. Documentation for prefetch_related:

Returns a new QuerySet instance that will prefetch the specified Many-To-One and Many-To-Many related objects when the QuerySet is evaluated.

When prefetch_related() is called more than once, the list of lookups to prefetch is appended to. If prefetch_related(None) is called, the list is cleared.

query
raw(raw_query, params=None, translations=None, using=None)

Returns the output of raw(...) after having applied the delayed operation.

reverse()[source]

Reverses the ordering of the DelayedQuerySet.

Note

We need to have a custom implementation for this because we want to reverse the ordering of the final queryset, not just the ordering within each component queryset.

select_for_update(nowait=False, skip_locked=False)

Raises NotImplementedError. Documentation for select_for_update:

Returns a new QuerySet instance that will select objects with a FOR UPDATE lock.

Returns the a new delayed queryset with select_related(...) having been called on each of the component querysets.: Documentation for select_related:

Returns a new QuerySet instance that will select related objects.

If fields are specified, they must be ForeignKey fields and only those related objects are included in the selection.

If select_related(None) is called, the list is cleared.

union(*other_qs, **kwargs)

Raises NotImplementedError.

update(**kwargs)

Raises NotImplementedError. Documentation for update:

Updates all elements in the current QuerySet, setting all the given fields to the appropriate values.

update_or_create(defaults=None, **kwargs)

Raises NotImplementedError. Documentation for update_or_create:

Looks up an object with the given kwargs, updating one with defaults if it exists, otherwise creates a new one. Returns a tuple (object, created), where created is a boolean specifying whether an object was created.

using(alias)

Returns the a new delayed queryset with using(...) having been called on each of the component querysets.: Documentation for using:

Selects which database this QuerySet should execute its query against.

values(*fields, **expressions)

Returns the a new delayed queryset with values(...) having been called on each of the component querysets.:

values_list(*fields, **kwargs)

Returns the a new delayed queryset with values_list(...) having been called on each of the component querysets.:

class django_delayed_union.DelayedUnionQuerySet(*querysets, **kwargs)[source]

Bases: django_delayed_union.base.DelayedQuerySet

distinct()[source]

Returns a new DelayedUnionQuerySet instance that will select only distinct results.

update(**kwargs)[source]

Updates all elements in the component querysets, setting all the given fields to the appropriate values. Returns the total number of (not-necessarily distinct) rows updated.

class django_delayed_union.DelayedIntersectionQuerySet(*querysets)[source]

Bases: django_delayed_union.base.DelayedQuerySet

distinct()[source]

Returns a new DelayedIntersectionQuerySet instance that will select only distinct results.

class django_delayed_union.DelayedDifferenceQuerySet(*querysets)[source]

Bases: django_delayed_union.base.DelayedQuerySet

distinct()[source]

Returns a new DelayedDifferenceQuerySet instance that will select only distinct results.