djblets.db.query¶
- class LocalDataQuerySet(data)¶
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 = re.compile('(-?)(\\w+)')¶
- __init__(data)¶
- all()¶
Returns a cloned copy of this queryset.
- clone()¶
Returns a cloned copy of this queryset.
- count()¶
Returns the number of items in this queryset.
- exclude(**kwargs)¶
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)¶
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)¶
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)¶
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)¶
- __getitem__(i)¶
- __getslice__(i, j)¶
- __iter__()¶
- __len__()¶
- get_object_or_none(cls, *args, **kwargs)¶
Return a model instance or None if one can not be found.
- Parameters:
cls (
type
ordjango.db.models.manager.Manager
) – Either the model, a subclass ofdjango.db.models.Model
, or its manager.*args (
tuple
) – Arguments to pass toget()
.**kwargs (
dict
) – Keyword arguments to pass toget()
.
- Returns:
The model instance, if it could be found, or
None
otherwise.- Return type:
- prefix_q(prefix, q, clone=True)¶
Prefix a query expression.
A
query expression
is used inManager.get
andManager.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 isFalse
,q
will be modified in place. This defaults toTrue
.
- 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))