Python django.db.models.expressions.OrderBy() Examples
The following are 13
code examples of django.db.models.expressions.OrderBy().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.db.models.expressions
, or try the search function
.
Example #1
Source File: queries.py From koku with GNU Affero General Public License v3.0 | 6 votes |
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)
Example #2
Source File: submissions.py From hypha with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #3
Source File: filters.py From caluma with GNU General Public License v3.0 | 6 votes |
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"), ), )
Example #4
Source File: filters.py From caluma with GNU General Public License v3.0 | 6 votes |
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, )
Example #5
Source File: compiler.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
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]
Example #6
Source File: test_ocp_query_handler.py From koku with GNU Affero General Public License v3.0 | 5 votes |
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)
Example #7
Source File: compiler.py From bioforum with MIT License | 5 votes |
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]
Example #8
Source File: compiler.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
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]
Example #9
Source File: main.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
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
Example #10
Source File: checks.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
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 []
Example #11
Source File: compiler.py From python with Apache License 2.0 | 5 votes |
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]
Example #12
Source File: compiler.py From openhgsenti with Apache License 2.0 | 5 votes |
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]
Example #13
Source File: compiler.py From python2017 with MIT License | 5 votes |
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]