diff options
author | David Wobrock <david.wobrock@gmail.com> | 2020-04-25 13:30:58 +0200 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-04-16 10:33:42 +0200 |
commit | fd325b9dee95f0930f5e8d582892b899508021fe (patch) | |
tree | 70d6a7a8b175d0d3968d594f5d69a6f468e2b039 /django/db/migrations/utils.py | |
parent | 725ca1fb60da2ef1bb6db146cd2d735591e75fbd (diff) | |
download | django-fd325b9dee95f0930f5e8d582892b899508021fe.tar.gz |
Refs #29899 -- Moved resolve_relation() to django.db.migrations.utils.
Diffstat (limited to 'django/db/migrations/utils.py')
-rw-r--r-- | django/db/migrations/utils.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/django/db/migrations/utils.py b/django/db/migrations/utils.py index 8939794e59..7ce2c5bc82 100644 --- a/django/db/migrations/utils.py +++ b/django/db/migrations/utils.py @@ -1,6 +1,8 @@ import datetime import re +from django.db.models.fields.related import RECURSIVE_RELATIONSHIP_CONSTANT + COMPILED_REGEX_TYPE = type(re.compile('')) @@ -15,3 +17,30 @@ class RegexObject: def get_migration_name_timestamp(): return datetime.datetime.now().strftime("%Y%m%d_%H%M") + + +def resolve_relation(model, app_label=None, model_name=None): + """ + Turn a model class or model reference string and return a model tuple. + + app_label and model_name are used to resolve the scope of recursive and + unscoped model relationship. + """ + if isinstance(model, str): + if model == RECURSIVE_RELATIONSHIP_CONSTANT: + if app_label is None or model_name is None: + raise TypeError( + 'app_label and model_name must be provided to resolve ' + 'recursive relationships.' + ) + return app_label, model_name + if '.' in model: + app_label, model_name = model.split('.', 1) + return app_label, model_name.lower() + if app_label is None: + raise TypeError( + 'app_label must be provided to resolve unscoped model ' + 'relationships.' + ) + return app_label, model.lower() + return model._meta.app_label, model._meta.model_name |