Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/db/models/utils.py: 73%
24 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1import functools
2from collections import namedtuple
5def make_model_tuple(model):
6 """
7 Take a model or a string of the form "app_label.ModelName" and return a
8 corresponding ("app_label", "modelname") tuple. If a tuple is passed in,
9 assume it's a valid model tuple already and return it unchanged.
10 """
11 try:
12 if isinstance(model, tuple): 12 ↛ 13line 12 didn't jump to line 13, because the condition on line 12 was never true
13 model_tuple = model
14 elif isinstance(model, str):
15 app_label, model_name = model.split(".")
16 model_tuple = app_label, model_name.lower()
17 else:
18 model_tuple = model._meta.app_label, model._meta.model_name
19 assert len(model_tuple) == 2
20 return model_tuple
21 except (ValueError, AssertionError):
22 raise ValueError(
23 "Invalid model reference '%s'. String model references "
24 "must be of the form 'app_label.ModelName'." % model
25 )
28def resolve_callables(mapping):
29 """
30 Generate key/value pairs for the given mapping where the values are
31 evaluated if they're callable.
32 """
33 for k, v in mapping.items():
34 yield k, v() if callable(v) else v
37def unpickle_named_row(names, values):
38 return create_namedtuple_class(*names)(*values)
41@functools.lru_cache()
42def create_namedtuple_class(*names):
43 # Cache type() with @lru_cache() since it's too slow to be called for every
44 # QuerySet evaluation.
45 def __reduce__(self):
46 return unpickle_named_row, (names, tuple(self))
48 return type(
49 "Row",
50 (namedtuple("Row", names),),
51 {"__reduce__": __reduce__, "__slots__": ()},
52 )