Jump to >

djblets.db.query

class LocalDataQuerySet(data)[source]

Bases: object

A QuerySet that operates on generic data provided by the caller.

This can be used in some circumstances when code requires a QuerySet, but where the data doesn’t come from the database. The caller can instantiate one of these and provide it.

This doesn’t perform full support for all of QuerySet’s abilities. It does, however, support the following basic functions:

  • all
  • clone
  • count
  • exclude
  • filter
  • get
  • order_by
  • prefetch_related
  • select_related

As well as the operators expected by consumers of QuerySet, such as __len__ and __iter__.

This is particularly handy with WebAPIResource.

PROPERTY_REGEX = <_sre.SRE_Pattern object>[source]
__init__(data)[source]

x.__init__(…) initializes x; see help(type(x)) for signature

all()[source]

Returns a cloned copy of this queryset.

clone()[source]

Returns a cloned copy of this queryset.

count()[source]

Returns the number of items in this queryset.

exclude(**kwargs)[source]

Returns a queryset excluding items from this queryset.

The result will be a LocalDataQuerySet that contains all items from this queryset that do not contain attributes with values matching those that were passed to this function as keyword arguments.

filter(**kwargs)[source]

Returns a queryset filtering items from this queryset.

The result will be a LocalDataQuerySet that contains all items from this queryset that contain attributes with values matching those that were passed to this function as keyword arguments.

get(**kwargs)[source]

Returns a single result from this queryset.

This will return a single result from the list of items in this queryset. If keyword arguments are provided, they will be used to filter the queryset down.

There must be only one item in the queryset matching the given criteria, or a MultipleObjectsReturned will be raised. If there are no items, then an ObjectDoesNotExist will be raised.

order_by(*attrs, **kwargs)[source]

Returns a queryset ordering items by the specified attributes.

The result will be a LocalDataQuerySet that contains all items from this queryset ordered by the attributes specified. If multiple attributes are specified, the items are sorted by the first attribute and ties are broken by the other following attributes.

All items are sorted in ascending order. To specify a descending order, an attribute must have a ‘-‘ prepended to the name, such as -attribute_A.

Stub for compatibility with QuerySet.prefetch_related.

This will simply return a clone of this queryset.

Stub for compatibility with QuerySet.select_related.

This will simply return a clone of this queryset.

__contains__(i)[source]
__getitem__(i)[source]
__getslice__(i, j)[source]
__iter__()[source]
__len__()[source]

Allow for chaining select_related calls on a queryset.

This is a workaround for Django 1.6’s inability to chain calls to QuerySet.select_related(). When using Django 1.6, the returned queryset will be a special type that handles the chaining. On Django 1.7 and higher, querysets support this natively, so this function will just return the queryset as-is.

Parameters:queryset (django.db.models.query.QuerySet) – The queryset to make chainable.
Returns:A queryset that allows for chaining.
Return type:django.db.models.query.QuerySet
get_object_or_none(cls, *args, **kwargs)[source]

Return a model instance or None if one can not be found.

Parameters:
  • cls (type or django.db.models.manager.Manager) – Either the model, a subclass of django.db.models.Model, or its manager.
  • *args (tuple) – Arguments to pass to get().
  • **kwargs (dict) – Keyword arguments to pass to get().
Returns:

The model instance, if it could be found, or None otherwise.

Return type:

django.db.models.Model

prefix_q(prefix, q, clone=True)[source]

Prefix a query expression.

A query expression is used in Manager.get and Manager.filter methods. These expressions are made of a tree of key-value pairs.

Prefixing these expressions allows a query expression for one model to be used in a related model. The resulting prefixed expression will have every key in every key-value pair prefixed with the given prefix.

By default, the query expression will be cloned. That is, the given query expression will not be modified and a prefixed copy will be returned.

Parameters:
  • prefix (unicode) – The prefix to add to each member in the expression. This should be the name of a field and will be automatically suffixed by __.
  • q (django.db.models.query_utils.Q) – The expression to prefix.
  • clone (bool) – Determine if the query should be cloned. If this is False, q will be modified in place. This defaults to True.
Returns:

The prefixed query expression.

Return type:

django.db.models.query_utils.Q

Example usage:
from django.db import models
from django.db.query_utils import Q
from djblets.db.query import prefix_q

# Given the following models:
class A(models.Model):
    foo = models.IntegerField()
    bar = models.IntegerField()

class B(models.Model):
    fk = models.ForeignKey(A, on_delete=models.CASCADE)
    baz = models.IntegerField()

# And the following query expressions:
a_q = Q(foo=1) & Q(bar=2)
b_q = Q(fk__foo=1) & Q(fk__bar=2)

# The following queries are equivalent:
B.objects.filter(b_q)
B.objects.filter(prefix_q('fk', a_q))