本文整理汇总了Python中django.db.models.expressions.OrderBy方法的典型用法代码示例。如果您正苦于以下问题:Python expressions.OrderBy方法的具体用法?Python expressions.OrderBy怎么用?Python expressions.OrderBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.expressions
的用法示例。
在下文中一共展示了expressions.OrderBy方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_tag_order_by
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def get_tag_order_by(self, tag):
"""Generate an OrderBy clause forcing JSON column->key to be used.
This is only for helping to create a Window() for purposes of grouping
by tag.
Args:
tag (str): The Django formatted tag string
Ex. pod_labels__key
Returns:
OrderBy: A Django OrderBy clause using raw SQL
"""
descending = True if self.order_direction == "desc" else False
tag_column, tag_value = tag.split("__")
return OrderBy(RawSQL(f"{tag_column} -> %s", (tag_value,)), descending=descending)
示例2: order_by
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def order_by(self, *field_names):
if not self.json_field:
raise ValueError(
'json_field cannot be blank, please provide a field on which to perform the ordering'
)
def build_json_order_by(field):
try:
if field.replace('-', '') not in NAMED_BLOCKS:
return field
except AttributeError:
return field
if field[0] == '-':
descending = True
field = field[1:]
else:
descending = False
db_table = self.model._meta.db_table
return OrderBy(RawSQL(f'LOWER({db_table}.{self.json_field}->>%s)', (field,)), descending=descending, nulls_last=True)
field_ordering = [build_json_order_by(field) for field in field_names]
return super().order_by(*field_ordering)
示例3: _order_part
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def _order_part(self, qs, ord, filter_coll):
direction = ord.pop("direction", "ASC")
assert len(ord) == 1
filt_name = list(ord.keys())[0]
filter = filter_coll.get_filters()[filt_name]
qs, field = filter.get_ordering_value(qs, ord[filt_name])
# Normally, people use ascending order, and in this context it seems
# natural to have NULL entries at the end.
# Making the `nulls_first`/`nulls_last` parameter accessible in the
# GraphQL interface would be overkill, at least for now.
return (
qs,
OrderBy(
field,
descending=(direction == "DESC"),
nulls_first=(direction == "DESC"),
nulls_last=(direction == "ASC"),
),
)
示例4: get_ordering_value
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def get_ordering_value(self, param):
if not any(param.startswith(prefix) for prefix in ("meta_", "-meta_")):
return super().get_ordering_value(param)
descending = False
if param.startswith("-"):
descending = True
param = param[1:]
meta_field_key = param[5:]
# order_by works on json field keys only without dashes
# but we want to support dasherized keys as well as this is
# valid json, hence need to use raw sql
return OrderBy(
RawSQL(f'"{self.model._meta.db_table}"."meta"->%s', (meta_field_key,)),
descending=descending,
)
示例5: find_ordering_name
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def find_ordering_name(self, name, opts, alias=None, default_order='ASC',
already_seen=None):
"""
Returns the table alias (the name might be ambiguous, the alias will
not be) and column name for ordering by the given 'name' parameter.
The 'name' is of the form 'field1__field2__...__fieldN'.
"""
name, order = get_order_dir(name, default_order)
descending = True if order == 'DESC' else False
pieces = name.split(LOOKUP_SEP)
field, targets, alias, joins, path, opts = self._setup_joins(pieces, opts, alias)
# If we get to this point and the field is a relation to another model,
# append the default ordering for that model unless the attribute name
# of the field is specified.
if field.rel and path and opts.ordering and name != field.attname:
# Firstly, avoid infinite loops.
if not already_seen:
already_seen = set()
join_tuple = tuple(self.query.alias_map[j].table_name for j in joins)
if join_tuple in already_seen:
raise FieldError('Infinite loop caused by ordering.')
already_seen.add(join_tuple)
results = []
for item in opts.ordering:
results.extend(self.find_ordering_name(item, opts, alias,
order, already_seen))
return results
targets, alias, _ = self.query.trim_joins(targets, joins, path)
return [(OrderBy(t.get_col(alias), descending=descending), False) for t in targets]
示例6: test_get_tag_order_by
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def test_get_tag_order_by(self):
"""Verify that a propery order by is returned."""
tag = "pod_labels__key"
expected_param = (tag.split("__")[1],)
url = "?"
query_params = self.mocked_query_params(url, OCPCpuView)
handler = OCPReportQueryHandler(query_params)
result = handler.get_tag_order_by(tag)
expression = result.expression
self.assertIsInstance(result, OrderBy)
self.assertEqual(expression.sql, "pod_labels -> %s")
self.assertEqual(expression.params, expected_param)
示例7: find_ordering_name
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def find_ordering_name(self, name, opts, alias=None, default_order='ASC',
already_seen=None):
"""
Return the table alias (the name might be ambiguous, the alias will
not be) and column name for ordering by the given 'name' parameter.
The 'name' is of the form 'field1__field2__...__fieldN'.
"""
name, order = get_order_dir(name, default_order)
descending = True if order == 'DESC' else False
pieces = name.split(LOOKUP_SEP)
field, targets, alias, joins, path, opts = self._setup_joins(pieces, opts, alias)
# If we get to this point and the field is a relation to another model,
# append the default ordering for that model unless the attribute name
# of the field is specified.
if field.is_relation and opts.ordering and getattr(field, 'attname', None) != name:
# Firstly, avoid infinite loops.
if not already_seen:
already_seen = set()
join_tuple = tuple(getattr(self.query.alias_map[j], 'join_cols', None) for j in joins)
if join_tuple in already_seen:
raise FieldError('Infinite loop caused by ordering.')
already_seen.add(join_tuple)
results = []
for item in opts.ordering:
results.extend(self.find_ordering_name(item, opts, alias,
order, already_seen))
return results
targets, alias, _ = self.query.trim_joins(targets, joins, path)
return [(OrderBy(t.get_col(alias), descending=descending), False) for t in targets]
示例8: find_ordering_name
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def find_ordering_name(self, name, opts, alias=None, default_order='ASC',
already_seen=None):
"""
Return the table alias (the name might be ambiguous, the alias will
not be) and column name for ordering by the given 'name' parameter.
The 'name' is of the form 'field1__field2__...__fieldN'.
"""
name, order = get_order_dir(name, default_order)
descending = order == 'DESC'
pieces = name.split(LOOKUP_SEP)
field, targets, alias, joins, path, opts, transform_function = self._setup_joins(pieces, opts, alias)
# If we get to this point and the field is a relation to another model,
# append the default ordering for that model unless the attribute name
# of the field is specified.
if field.is_relation and opts.ordering and getattr(field, 'attname', None) != name:
# Firstly, avoid infinite loops.
already_seen = already_seen or set()
join_tuple = tuple(getattr(self.query.alias_map[j], 'join_cols', None) for j in joins)
if join_tuple in already_seen:
raise FieldError('Infinite loop caused by ordering.')
already_seen.add(join_tuple)
results = []
for item in opts.ordering:
results.extend(self.find_ordering_name(item, opts, alias,
order, already_seen))
return results
targets, alias, _ = self.query.trim_joins(targets, joins, path)
return [(OrderBy(transform_function(t, alias), descending=descending), False) for t in targets]
示例9: get_ordering_field_columns
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def get_ordering_field_columns(self):
"""
Return an OrderedDict of ordering field column numbers and asc/desc.
"""
# We must cope with more than one column having the same underlying sort
# field, so we base things on column numbers.
ordering = self._get_default_ordering()
ordering_fields = OrderedDict()
if ORDER_VAR not in self.params:
# for ordering specified on ModelAdmin or model Meta, we don't know
# the right column numbers absolutely, because there might be more
# than one column associated with that ordering, so we guess.
for field in ordering:
if isinstance(field, (Combinable, OrderBy)):
if not isinstance(field, OrderBy):
field = field.asc()
if isinstance(field.expression, F):
order_type = 'desc' if field.descending else 'asc'
field = field.expression.name
else:
continue
elif field.startswith('-'):
field = field[1:]
order_type = 'desc'
else:
order_type = 'asc'
for index, attr in enumerate(self.list_display):
if self.get_ordering_field(attr) == field:
ordering_fields[index] = order_type
break
else:
for p in self.params[ORDER_VAR].split('.'):
none, pfx, idx = p.rpartition('-')
try:
idx = int(idx)
except ValueError:
continue # skip it
ordering_fields[idx] = 'desc' if pfx == '-' else 'asc'
return ordering_fields
示例10: _check_ordering_item
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def _check_ordering_item(self, obj, model, field_name, label):
""" Check that `ordering` refers to existing fields. """
if isinstance(field_name, (Combinable, OrderBy)):
if not isinstance(field_name, OrderBy):
field_name = field_name.asc()
if isinstance(field_name.expression, F):
field_name = field_name.expression.name
else:
return []
if field_name == '?' and len(obj.ordering) != 1:
return [
checks.Error(
"The value of 'ordering' has the random ordering marker '?', "
"but contains other fields as well.",
hint='Either remove the "?", or remove the other fields.',
obj=obj.__class__,
id='admin.E032',
)
]
elif field_name == '?':
return []
elif LOOKUP_SEP in field_name:
# Skip ordering in the format field1__field2 (FIXME: checking
# this format would be nice, but it's a little fiddly).
return []
else:
if field_name.startswith('-'):
field_name = field_name[1:]
if field_name == 'pk':
return []
try:
model._meta.get_field(field_name)
except FieldDoesNotExist:
return refer_to_missing_field(field=field_name, option=label, model=model, obj=obj, id='admin.E033')
else:
return []
示例11: find_ordering_name
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def find_ordering_name(self, name, opts, alias=None, default_order='ASC',
already_seen=None):
"""
Returns the table alias (the name might be ambiguous, the alias will
not be) and column name for ordering by the given 'name' parameter.
The 'name' is of the form 'field1__field2__...__fieldN'.
"""
name, order = get_order_dir(name, default_order)
descending = True if order == 'DESC' else False
pieces = name.split(LOOKUP_SEP)
field, targets, alias, joins, path, opts = self._setup_joins(pieces, opts, alias)
# If we get to this point and the field is a relation to another model,
# append the default ordering for that model unless the attribute name
# of the field is specified.
if field.is_relation and opts.ordering and getattr(field, 'attname', None) != name:
# Firstly, avoid infinite loops.
if not already_seen:
already_seen = set()
join_tuple = tuple(getattr(self.query.alias_map[j], 'join_cols', None) for j in joins)
if join_tuple in already_seen:
raise FieldError('Infinite loop caused by ordering.')
already_seen.add(join_tuple)
results = []
for item in opts.ordering:
results.extend(self.find_ordering_name(item, opts, alias,
order, already_seen))
return results
targets, alias, _ = self.query.trim_joins(targets, joins, path)
return [(OrderBy(t.get_col(alias), descending=descending), False) for t in targets]
示例12: find_ordering_name
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import OrderBy [as 别名]
def find_ordering_name(self, name, opts, alias=None, default_order='ASC',
already_seen=None):
"""
Returns the table alias (the name might be ambiguous, the alias will
not be) and column name for ordering by the given 'name' parameter.
The 'name' is of the form 'field1__field2__...__fieldN'.
"""
name, order = get_order_dir(name, default_order)
descending = True if order == 'DESC' else False
pieces = name.split(LOOKUP_SEP)
field, targets, alias, joins, path, opts = self._setup_joins(pieces, opts, alias)
# If we get to this point and the field is a relation to another model,
# append the default ordering for that model unless the attribute name
# of the field is specified.
if field.is_relation and path and opts.ordering and name != field.attname:
# Firstly, avoid infinite loops.
if not already_seen:
already_seen = set()
join_tuple = tuple(getattr(self.query.alias_map[j], 'join_cols', None) for j in joins)
if join_tuple in already_seen:
raise FieldError('Infinite loop caused by ordering.')
already_seen.add(join_tuple)
results = []
for item in opts.ordering:
results.extend(self.find_ordering_name(item, opts, alias,
order, already_seen))
return results
targets, alias, _ = self.query.trim_joins(targets, joins, path)
return [(OrderBy(t.get_col(alias), descending=descending), False) for t in targets]