Refs #29899 -- Moved resolve_relation() to django.db.migrations.utils.
This commit is contained in:
parent
725ca1fb60
commit
fd325b9dee
|
@ -1,13 +1,14 @@
|
|||
from django.db import models
|
||||
from django.db.migrations.operations.base import Operation
|
||||
from django.db.migrations.state import ModelState
|
||||
from django.db.migrations.utils import resolve_relation
|
||||
from django.db.models.options import normalize_together
|
||||
from django.utils.functional import cached_property
|
||||
|
||||
from .fields import (
|
||||
AddField, AlterField, FieldOperation, RemoveField, RenameField,
|
||||
)
|
||||
from .utils import field_references, get_references, resolve_relation
|
||||
from .utils import field_references, get_references
|
||||
|
||||
|
||||
def _check_for_duplicates(arg_name, objs):
|
||||
|
|
|
@ -1,34 +1,6 @@
|
|||
from collections import namedtuple
|
||||
|
||||
from django.db.models.fields.related import RECURSIVE_RELATIONSHIP_CONSTANT
|
||||
|
||||
|
||||
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
|
||||
|
||||
from django.db.migrations.utils import resolve_relation
|
||||
|
||||
FieldReference = namedtuple('FieldReference', 'to through')
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue