MCPcopy
hub / github.com/django/django / _check_ordering

Method _check_ordering

django/db/models/base.py:2276–2382  ·  view source on GitHub ↗

Check "ordering" option -- is it a list of strings and do all fields exist?

(cls)

Source from the content-addressed store, hash-verified

2274
2275 @classmethod
2276 def _check_ordering(cls):
2277 """
2278 Check "ordering" option -- is it a list of strings and do all fields
2279 exist?
2280 """
2281 if cls._meta._ordering_clash:
2282 return [
2283 checks.Error(
2284 "'ordering' and 'order_with_respect_to' cannot be used together.",
2285 obj=cls,
2286 id="models.E021",
2287 ),
2288 ]
2289
2290 if cls._meta.order_with_respect_to or not cls._meta.ordering:
2291 return []
2292
2293 if not isinstance(cls._meta.ordering, (list, tuple)):
2294 return [
2295 checks.Error(
2296 "'ordering' must be a tuple or list (even if you want to order by "
2297 "only one field).",
2298 obj=cls,
2299 id="models.E014",
2300 )
2301 ]
2302
2303 errors = []
2304 fields = cls._meta.ordering
2305
2306 # Skip expressions and '?' fields.
2307 fields = (f for f in fields if isinstance(f, str) and f != "?")
2308
2309 # Convert "-field" to "field".
2310 fields = (f.removeprefix("-") for f in fields)
2311
2312 # Separate related fields and non-related fields.
2313 _fields = []
2314 related_fields = []
2315 for f in fields:
2316 if LOOKUP_SEP in f:
2317 related_fields.append(f)
2318 else:
2319 _fields.append(f)
2320 fields = _fields
2321
2322 # Check related fields.
2323 for field in related_fields:
2324 _cls = cls
2325 fld = None
2326 for part in field.split(LOOKUP_SEP):
2327 try:
2328 # pk is an alias that won't be found by opts.get_field.
2329 if part == "pk":
2330 fld = _cls._meta.pk
2331 else:
2332 fld = _cls._meta.get_field(part)
2333 if fld.is_relation:

Callers 1

checkMethod · 0.45

Calls 7

related_query_nameMethod · 0.80
extendMethod · 0.80
appendMethod · 0.45
splitMethod · 0.45
get_fieldMethod · 0.45
get_transformMethod · 0.45
get_lookupMethod · 0.45

Tested by

no test coverage detected