MCPcopy
hub / github.com/django/django / add_ordering

Method add_ordering

django/db/models/sql/query.py:2326–2360  ·  view source on GitHub ↗

Add items from the 'ordering' sequence to the query's "order by" clause. These items are either field names (not column names) -- possibly with a direction prefix ('-' or '?') -- or OrderBy expressions. If 'ordering' is empty, clear all ordering from the que

(self, *ordering)

Source from the content-addressed store, hash-verified

2324 )
2325
2326 def add_ordering(self, *ordering):
2327 """
2328 Add items from the 'ordering' sequence to the query's "order by"
2329 clause. These items are either field names (not column names) --
2330 possibly with a direction prefix ('-' or '?') -- or OrderBy
2331 expressions.
2332
2333 If 'ordering' is empty, clear all ordering from the query.
2334 """
2335 errors = []
2336 for item in ordering:
2337 if isinstance(item, str):
2338 if item == "?":
2339 continue
2340 item = item.removeprefix("-")
2341 if item in self.annotations:
2342 continue
2343 if self.extra and item in self.extra:
2344 continue
2345 # names_to_path() validates the lookup. A descriptive
2346 # FieldError will be raise if it's not.
2347 self.names_to_path(item.split(LOOKUP_SEP), self.model._meta)
2348 elif not hasattr(item, "resolve_expression"):
2349 errors.append(item)
2350 if getattr(item, "contains_aggregate", False):
2351 raise FieldError(
2352 "Using an aggregate in order_by() without also including "
2353 "it in annotate() is not allowed: %s" % item
2354 )
2355 if errors:
2356 raise FieldError("Invalid order_by arguments: %s" % errors)
2357 if ordering:
2358 self.order_by += ordering
2359 else:
2360 self.default_ordering = False
2361
2362 @property
2363 def orderby_issubset_groupby(self):

Callers 2

_earliestMethod · 0.80
order_byMethod · 0.80

Calls 4

names_to_pathMethod · 0.95
FieldErrorClass · 0.90
splitMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected