本文整理汇总了Python中django.db.models.sql.constants.QUERY_TERMS类的典型用法代码示例。如果您正苦于以下问题:Python QUERY_TERMS类的具体用法?Python QUERY_TERMS怎么用?Python QUERY_TERMS使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QUERY_TERMS类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: monkey_patch_where_node
def monkey_patch_where_node():
global monkey_patched
if monkey_patched is True:
return
monkey_patched = True
if django.VERSION >= (1,5):
from django.db.models.sql.constants import QUERY_TERMS
QUERY_TERMS.update(UnaccentOperation.operators.keys() + UnaccentOperation.smart_operators)
# Update the operators accepted by a query when adding filters by adding those of unaccent
# This way, it passes the test at db/models/sql/query.py:1021 which otherwise will override our custom lookup_types
Query.query_terms.update(izip(UnaccentOperation.operators.keys() + UnaccentOperation.smart_operators, repeat(None)))
WhereNode.add = patched_wherenode_add
示例2: QT
def QT(field_lookup, value):
lookup_pos = -1
for lookup in QUERY_TERMS.keys():
ltemp = field_lookup.rfind(lookup)
if ltemp != -1:
lookup_pos = ltemp
break
# we have a lookup
if lookup_pos != -1:
field_ = field_lookup[:lookup_pos-3]
lookup = field_lookup[lookup_pos-3:]
else:
field_ = field_lookup
lookup = ''
field_pos = field_.rfind('__')
# we don't have a relationship
if field_pos == -1:
field = field_
else:
field = field_[field_.rfind('__')+2:]
relationship = field_[:field_.rfind(field)]
field = format_field_name(field)
q = relationship + field + lookup
kwargs = {str(q): value}
return Q(**kwargs)
示例3: build_filters
def build_filters(self, filters=None):
"""
Given a dictionary of filters, create the necessary ORM-level filters.
Keys should be resource fields, **NOT** model fields.
Valid values are either a list of Django filter types (i.e.
``['startswith', 'exact', 'lte']``), the ``ALL`` constant or the
``ALL_WITH_RELATIONS`` constant.
"""
# At the declarative level:
# filtering = {
# 'resource_field_name': ['exact', 'startswith', 'endswith', 'contains'],
# 'resource_field_name_2': ['exact', 'gt', 'gte', 'lt', 'lte', 'range'],
# 'resource_field_name_3': ALL,
# 'resource_field_name_4': ALL_WITH_RELATIONS,
# ...
# }
# Accepts the filters as a dict. None by default, meaning no filters.
if filters is None:
filters = {}
qs_filters = {}
for filter_expr, value in filters.items():
filter_bits = filter_expr.split(LOOKUP_SEP)
field_name = filter_bits.pop(0)
filter_type = 'exact'
if not field_name in self.fields:
# It's not a field we know about. Move along citizen.
continue
if len(filter_bits) and filter_bits[-1] in QUERY_TERMS.keys():
filter_type = filter_bits.pop()
lookup_bits = self.check_filtering(field_name, filter_type, filter_bits)
if value in ['true', 'True', True]:
value = True
elif value in ['false', 'False', False]:
value = False
elif value in ('nil', 'none', 'None', None):
value = None
# Split on ',' if not empty string and either an in or range filter.
if filter_type in ('in', 'range') and len(value):
if hasattr(filters, 'getlist'):
value = filters.getlist(filter_expr)
else:
value = value.split(',')
db_field_name = LOOKUP_SEP.join(lookup_bits)
qs_filter = "%s%s%s" % (db_field_name, LOOKUP_SEP, filter_type)
qs_filters[qs_filter] = value
return dict_strip_unicode_keys(qs_filters)
示例4: build_filters
def build_filters(self, filters=None):
if filters is None:
filters = {}
qs_filters = {}
for filter_expr, value in filters.items():
filter_bits = filter_expr.split(LOOKUP_SEP)
field_name = filter_bits.pop(0)
filter_type = 'exact'
if not field_name in self.fields:
# It's not a field we know about. Move along citizen.
continue
if len(filter_bits) and filter_bits[-1] in QUERY_TERMS.keys():
filter_type = filter_bits.pop()
lookup_bits = self.check_filtering(field_name, filter_type, filter_bits)
# Is the field defined in our resource?
field = getattr(self, field_name)
# If we have a field defined use its type conversion
if field:
value = field.convert(value)
elif isinstance(value, basestring):
if value.lower() in ('true', 'yes'):
value = True
elif value.lower() in ('false', 'no'):
value = False
elif value.lower() in ('nil', 'none', 'null'):
value = None
# elif value.isnumeric():
# value = int(value)
# Split on ',' if not empty string and either an in or range filter.
if filter_type in ('in', 'range') and len(value):
if hasattr(filters, 'getlist'):
value = filters.getlist(filter_expr)
else:
value = value.split(',')
qs_filters[field_name] = {
'filter_type': filter_type,
'value': value
}
return dict_strip_unicode_keys(qs_filters)
示例5: build_filters
def build_filters(self, filters=None):
if not filters: return { }
# Replace enumeration keys with the right values.
from django.db.models.sql.constants import QUERY_TERMS, LOOKUP_SEP
f = { }
for k, v in filters.items():
path = k.split(LOOKUP_SEP)
if len(path) and path[-1] in QUERY_TERMS.keys(): path.pop()
model, field = self.find_field(path)
if model:
enum = model.Meta.queryset.model._meta.get_field(field).choices
if GBaseModel.is_enum(enum):
v = int(enum.by_key(v))
f[k] = v
return super(GBaseModel, self).build_filters(filters=f)
示例6: _expand_money_params
def _expand_money_params(kwargs):
from moneyed import Money
from django.db.models.sql.constants import LOOKUP_SEP, QUERY_TERMS
to_append = {}
for name, value in kwargs.items():
if isinstance(value, Money):
# Get rid of __lt, __gt etc for the currency lookup
path = name.split(LOOKUP_SEP)
if QUERY_TERMS.has_key(path[-1]):
clean_name = LOOKUP_SEP.join(path[:-1])
else:
clean_name = name
to_append[name] = value.amount
to_append[currency_field_name(clean_name)] = smart_unicode(value.currency)
kwargs.update(to_append)
return kwargs
示例7: build_filters
def build_filters(self, filters=None):
# Override the filters so we can stop Tastypie silently ignoring
# invalid filters. That will cause an invalid filtering just to return
# lots of results.
if filters is None:
filters = {}
qs_filters = {}
for filter_expr, value in filters.items():
filter_bits = filter_expr.split(LOOKUP_SEP)
field_name = filter_bits.pop(0)
filter_type = 'exact'
if not field_name in self.fields:
# Don't just ignore this. Tell the world. Shame I have to
# override all this, just to do this.
raise InvalidFilterError('Not a valid filtering field: %s'
% field_name)
if len(filter_bits) and filter_bits[-1] in QUERY_TERMS.keys():
filter_type = filter_bits.pop()
lookup_bits = self.check_filtering(field_name, filter_type,
filter_bits)
if value in ['true', 'True', True]:
value = True
elif value in ['false', 'False', False]:
value = False
elif value in ('nil', 'none', 'None', None):
value = None
# Split on ',' if not empty string and either an in or range
# filter.
if filter_type in ('in', 'range') and len(value):
if hasattr(filters, 'getlist'):
value = filters.getlist(filter_expr)
else:
value = value.split(',')
db_field_name = LOOKUP_SEP.join(lookup_bits)
qs_filter = "%s%s%s" % (db_field_name, LOOKUP_SEP, filter_type)
qs_filters[qs_filter] = value
return dict_strip_unicode_keys(qs_filters)
示例8: filters_for_model
def filters_for_model(model, fields=None, exclude=None, filter_for_field=None):
field_dict = SortedDict()
opts = model._meta
if fields is None:
fields = [f.name for f in sorted(opts.fields + opts.many_to_many)]
for f in fields:
if exclude is not None and f in exclude:
continue
if f.split(LOOKUP_SEP)[-1] in QUERY_TERMS.keys():
lookup_type = f.split(LOOKUP_SEP)[-1]
f = LOOKUP_SEP.join(f.split(LOOKUP_SEP)[:-1])
else:
lookup_type = None
field = get_model_field(model, f)
if field is None:
field_dict[f] = None
continue
filter_ = filter_for_field(field, f, lookup_type)
if filter_:
field_dict[f] = filter_
return field_dict
示例9: dict
'strictly_above',
'notextend_above',
'notextend_below',
'is_below',
'is_above',
'intersects',
'is_horizontal',
'is_perpendicular',
'is_parallel',
'contained_in_or_on',
'same_as',
'contains',
'center',
)
GEOMETRIC_LOOKUPS = dict((x, None) for x in geometric_lookups)
FTS_LOCKUPS = {'query': None, 'query_raw':None}
VARCHAR_LOOKUPS = dict((x, None) for x in ('unaccent', 'iunaccent'))
INTERVAL_LOOKUPS = dict((x, None) for x in ('distinct', 'exact', 'gt','lt','gte', 'lte'))
ARRAY_LOOKUPS = dict((x, None) for x in ('indexexact', 'distinct', 'slice', 'contains',
'containedby', 'overlap', 'exact', 'gt','lt','gte', 'lte'))
QUERY_TERMS.update(GEOMETRIC_LOOKUPS)
QUERY_TERMS.update(FTS_LOCKUPS)
QUERY_TERMS.update(VARCHAR_LOOKUPS)
QUERY_TERMS.update(INTERVAL_LOOKUPS)
QUERY_TERMS.update(ARRAY_LOOKUPS)
GEOMETRIC_TYPES = dict((x, None) for x in \
('box', 'point', 'line', 'lseg', 'path', 'polygon', 'circle'))
示例10: return
lhs_sql = 'UPPER(%s)' % lhs_sql
return (lhs_sql, params)
def get_rhs_op(self, connection, rhs):
return connection.operators['icontains'] % rhs
Field.register_lookup(Like)
Field.register_lookup(ILike)
else:
from django.db import connection
from django.db.models.fields import Field, subclassing
from django.db.models.sql.constants import QUERY_TERMS
if isinstance(QUERY_TERMS, set):
QUERY_TERMS.add('like')
QUERY_TERMS.add('ilike')
else:
QUERY_TERMS['like'] = None
QUERY_TERMS['ilike'] = None
connection.operators['like'] = connection.operators['contains']
connection.operators['ilike'] = connection.operators['icontains']
NEW_LOOKUP_TYPE = ('like', 'ilike')
def get_prep_lookup(self, lookup_type, value):
try:
return self.get_prep_lookup_origin(lookup_type, value)
except TypeError as e:
if lookup_type in NEW_LOOKUP_TYPE:
return value
示例11: build_filters
def build_filters(self, filters=None):
"""
Given a dictionary of filters, create the necessary ORM-level filters.
Keys should be resource fields, **NOT** model fields.
Valid values are either a list of Django filter types (i.e.
``['startswith', 'exact', 'lte']``), the ``ALL`` constant or the
``ALL_WITH_RELATIONS`` constant.
"""
# At the declarative level:
# filtering = {
# 'resource_field_name': ['exact', 'startswith', 'endswith', 'contains'],
# 'resource_field_name_2': ['exact', 'gt', 'gte', 'lt', 'lte', 'range'],
# 'resource_field_name_3': ALL,
# 'resource_field_name_4': ALL_WITH_RELATIONS,
# ...
# }
# Accepts the filters as a dict. None by default, meaning no filters.
if filters is None:
filters = {}
qs_filters = {}
for filter_expr, value in filters.items():
filter_bits = filter_expr.split(LOOKUP_SEP)
if not filter_bits[0] in self.fields:
# It's not a field we know about. Move along citizen.
continue
if not filter_bits[0] in self._meta.filtering:
raise InvalidFilterError("The '%s' field does not allow filtering." % filter_bits[0])
if filter_bits[-1] in QUERY_TERMS.keys():
filter_type = filter_bits.pop()
else:
filter_type = 'exact'
# Check to see if it's allowed lookup type.
if not self._meta.filtering[filter_bits[0]] in (ALL, ALL_WITH_RELATIONS):
# Must be an explicit whitelist.
if not filter_type in self._meta.filtering[filter_bits[0]]:
raise InvalidFilterError("'%s' is not an allowed filter on the '%s' field." % (filter_expr, filter_bits[0]))
# Check to see if it's a relational lookup and if that's allowed.
if len(filter_bits) > 1:
if not self._meta.filtering[filter_bits[0]] == ALL_WITH_RELATIONS:
raise InvalidFilterError("Lookups are not allowed more than one level deep on the '%s' field." % filter_bits[0])
if self.fields[filter_bits[0]].attribute is None:
raise InvalidFilterError("The '%s' field has no 'attribute' for searching with." % filter_bits[0])
if value == 'true':
value = True
elif value == 'false':
value = False
elif value in ('nil', 'none', 'None'):
value = None
db_field_name = LOOKUP_SEP.join([self.fields[filter_bits[0]].attribute] + filter_bits[1:])
qs_filter = "%s%s%s" % (db_field_name, LOOKUP_SEP, filter_type)
qs_filters[qs_filter] = value
return dict_strip_unicode_keys(qs_filters)
示例12: build_parameters_from_filters
def build_parameters_from_filters(self, prefix="", method="GET"):
parameters = []
# Deal with the navigational filters.
# Always add the limits & offset params on the root ( aka not prefixed ) object.
if not prefix and method.upper() == "GET":
navigation_filters = [
("limit", "int", "Specify the number of element to display per page."),
("offset", "int", "Specify the offset to start displaying element on a page."),
]
for name, type, desc in navigation_filters:
parameters.append(
self.build_parameter(
paramType="query", name=name, dataType=type, required=False, description=force_unicode(desc)
)
)
if "filtering" in self.schema and method.upper() == "GET":
for name, field in self.schema["filtering"].items():
# Integer value means this points to a related model
if field in [ALL, ALL_WITH_RELATIONS]:
if field == ALL: # TODO: Show all possible ORM filters for this field
# This code has been mostly sucked from the tastypie lib
if getattr(self.resource._meta, "queryset", None) is not None:
# Get the possible query terms from the current QuerySet.
if hasattr(self.resource._meta.queryset.query.query_terms, "keys"):
# Django 1.4 & below compatibility.
field = self.resource._meta.queryset.query.query_terms.keys()
else:
# Django 1.5+.
field = self.resource._meta.queryset.query.query_terms
else:
if hasattr(QUERY_TERMS, "keys"):
# Django 1.4 & below compatibility.
field = QUERY_TERMS.keys()
else:
# Django 1.5+.
field = QUERY_TERMS
elif field == ALL_WITH_RELATIONS: # Show all params from related model
# Add a subset of filter only foreign-key compatible on the relation itself.
# We assume foreign keys are only int based.
field = [
"gt",
"in",
"gte",
"lt",
"lte",
"exact",
] # TODO This could be extended by checking the actual type of the relational field, but afaik it's also an issue on tastypie.
related_resource = self.resource.fields[name].get_related_resource(None)
related_mapping = ResourceSwaggerMapping(related_resource)
parameters.extend(
related_mapping.build_parameters_from_filters(prefix="%s%s__" % (prefix, name))
)
if isinstance(field, list):
# Skip if this is an incorrect filter
if name not in self.schema["fields"]:
continue
schema_field = self.schema["fields"][name]
for query in field:
if query == "exact":
description = force_unicode(schema_field["help_text"])
dataType = schema_field["type"]
# Use a better description for related models with exact filter
if dataType == "related":
# Assume that related pk is an integer
# TODO if youre not using integer ID for pk then we need to look this up somehow
dataType = "integer"
description = "ID of related resource"
parameters.append(
self.build_parameter(
paramType="query",
name="%s%s" % (prefix, name),
dataType=dataType,
required=False,
description=description,
)
)
else:
parameters.append(
self.build_parameter(
paramType="query",
name="%s%s__%s" % (prefix, name, query),
dataType=schema_field["type"],
required=False,
description=force_unicode(schema_field["help_text"]),
)
)
return parameters
示例13: center
'center', # center(object)
'area', # area(object)
'area_gt',
'area_lt',
'area_gte',
'area_lte',
'overlap',
'strictly_left_of',
'strictly_right_of',
'notextendto_right_of',
'notextendto_left_of',
'strictly_below',
'strictly_above',
'notextend_above',
'notextend_below',
'is_below',
'is_above',
'intersects',
'is_horizontal',
'is_perpendicular',
'is_parallel',
'contained_in_or_on',
'contains',
'same_as',
)
QUERY_TERMS.update(dict([(x, None) for x in \
('indexexact', 'distinct', 'slice', 'containedby', 'unaccent', 'iunaccent', 'query')]))
QUERY_TERMS.update(dict([(x, None) for x in geometric_lookups]))
示例14: monkey_get_db_prep_lookup
return value_returned
def monkey_get_db_prep_lookup(cls):
cls.get_db_prep_lookup_origin = cls.get_db_prep_lookup
cls.get_db_prep_lookup = get_db_prep_lookup
if hasattr(subclassing, "call_with_connection_and_prepared"): # Dj > 1.1
setattr(cls, "get_db_prep_lookup", subclassing.call_with_connection_and_prepared(cls.get_db_prep_lookup))
for new_cls in cls.__subclasses__():
monkey_get_db_prep_lookup(new_cls)
if backend_allowed:
if isinstance(QUERY_TERMS, set):
QUERY_TERMS.add("similar")
else:
QUERY_TERMS["similar"] = None
if backend_allowed == "postgis":
if isinstance(ALL_TERMS, set):
ALL_TERMS.add("similar")
else:
ALL_TERMS["similar"] = None
connection.operators["similar"] = "%%%% %s"
NEW_LOOKUP_TYPE = ("similar",)
monkey_get_db_prep_lookup(Field)
if hasattr(Field, "get_prep_lookup"):
示例15: sorted
from django.utils.translation import ugettext_lazy as _
from django_filters.fields import NumericRangeField, DateRangeField, TimeRangeField, LookupTypeField
__all__ = [
'Filter', 'CharFilter', 'BooleanFilter', 'ChoiceFilter',
'MultipleChoiceFilter', 'DateFilter', 'DateTimeFilter', 'TimeFilter',
'ModelChoiceFilter', 'ModelMultipleChoiceFilter', 'NumberFilter',
'RangeFilter', 'DateRangeFilter', 'AllValuesFilter',
'OpenRangeNumericFilter', 'OpenRangeDateFilter', 'OpenRangeTimeFilter'
]
LOOKUP_TYPES = None
try:
LOOKUP_TYPES = sorted(QUERY_TERMS.keys()) # Django 1.4
except AttributeError:
LOOKUP_TYPES = sorted(list(QUERY_TERMS)) # Django >= 1.5
class Filter(object):
creation_counter = 0
field_class = forms.Field
def __init__(self, name=None, label=None, widget=None, action=None,
lookup_type='exact', required=False, **kwargs):
self.name = name
self.label = label
if action:
self.filter = action
self.lookup_type = lookup_type
self.widget = widget