Plain is headed towards 1.0! Subscribe for development updates →

 1import functools
 2from collections import namedtuple
 3
 4
 5def make_model_tuple(model):
 6    """
 7    Take a model or a string of the form "package_label.ModelName" and return a
 8    corresponding ("package_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):
13            model_tuple = model
14        elif isinstance(model, str):
15            package_label, model_name = model.split(".")
16            model_tuple = package_label, model_name.lower()
17        else:
18            model_tuple = model._meta.package_label, model._meta.model_name
19        assert len(model_tuple) == 2
20        return model_tuple
21    except (ValueError, AssertionError):
22        raise ValueError(
23            f"Invalid model reference '{model}'. String model references "
24            "must be of the form 'package_label.ModelName'."
25        )
26
27
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
35
36
37def unpickle_named_row(names, values):
38    return create_namedtuple_class(*names)(*values)
39
40
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))
47
48    return type(
49        "Row",
50        (namedtuple("Row", names),),
51        {"__reduce__": __reduce__, "__slots__": ()},
52    )