2015-03-03 16:43:56 +08:00
|
|
|
def make_model_tuple(model):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Take a model or a string of the form "app_label.ModelName" and return a
|
2015-03-03 16:43:56 +08:00
|
|
|
corresponding ("app_label", "modelname") tuple. If a tuple is passed in,
|
2017-01-25 07:04:12 +08:00
|
|
|
assume it's a valid model tuple already and return it unchanged.
|
2015-03-03 16:43:56 +08:00
|
|
|
"""
|
2016-05-18 23:18:40 +08:00
|
|
|
try:
|
|
|
|
if isinstance(model, tuple):
|
|
|
|
model_tuple = model
|
2016-12-29 23:27:49 +08:00
|
|
|
elif isinstance(model, str):
|
2016-05-18 23:18:40 +08:00
|
|
|
app_label, model_name = model.split(".")
|
|
|
|
model_tuple = app_label, model_name.lower()
|
|
|
|
else:
|
|
|
|
model_tuple = model._meta.app_label, model._meta.model_name
|
|
|
|
assert len(model_tuple) == 2
|
|
|
|
return model_tuple
|
|
|
|
except (ValueError, AssertionError):
|
|
|
|
raise ValueError(
|
|
|
|
"Invalid model reference '%s'. String model references "
|
|
|
|
"must be of the form 'app_label.ModelName'." % model
|
|
|
|
)
|
2019-11-30 00:30:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def resolve_callables(mapping):
|
|
|
|
"""
|
|
|
|
Generate key/value pairs for the given mapping where the values are
|
|
|
|
evaluated if they're callable.
|
|
|
|
"""
|
|
|
|
for k, v in mapping.items():
|
|
|
|
yield k, v() if callable(v) else v
|