mirror of
https://github.com/django/django.git
synced 2025-09-26 20:19:16 +00:00
Introduced ModelTuple to remove migrations boilerplate.
This commit is contained in:
parent
ad82900ad9
commit
013bcf57d5
4 changed files with 62 additions and 60 deletions
|
@ -7,6 +7,7 @@ from django.utils.functional import cached_property
|
|||
from .fields import (
|
||||
AddField, AlterField, FieldOperation, RemoveField, RenameField,
|
||||
)
|
||||
from .utils import ModelTuple, field_references_model
|
||||
|
||||
|
||||
def _check_for_duplicates(arg_name, objs):
|
||||
|
@ -104,19 +105,15 @@ class CreateModel(ModelOperation):
|
|||
return True
|
||||
|
||||
# Check we didn't inherit from the model
|
||||
models_to_check = [
|
||||
base for base in self.bases
|
||||
if base is not models.Model and isinstance(base, (models.base.ModelBase, str))
|
||||
]
|
||||
model_tuple = ModelTuple(app_label, name_lower)
|
||||
for base in self.bases:
|
||||
if (base is not models.Model and isinstance(base, (models.base.ModelBase, str)) and
|
||||
ModelTuple.from_model(base) == model_tuple):
|
||||
return True
|
||||
|
||||
# Check we have no FKs/M2Ms with it
|
||||
for fname, field in self.fields:
|
||||
if field.remote_field:
|
||||
models_to_check.append(field.remote_field.model)
|
||||
# Now go over all the models and check against them
|
||||
for model in models_to_check:
|
||||
model_app_label, model_name = self.model_to_key(model)
|
||||
if (model_name == name_lower and app_label is None or
|
||||
not model_app_label or model_app_label == app_label):
|
||||
for _name, field in self.fields:
|
||||
if field_references_model(field, model_tuple):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
@ -267,7 +264,7 @@ class RenameModel(ModelOperation):
|
|||
renamed_model.name = self.new_name
|
||||
state.models[app_label, self.new_name_lower] = renamed_model
|
||||
# Repoint all fields pointing to the old model to the new one.
|
||||
old_model_tuple = app_label, self.old_name_lower
|
||||
old_model_tuple = ModelTuple(app_label, self.old_name_lower)
|
||||
new_remote_model = '%s.%s' % (app_label, self.new_name)
|
||||
to_reload = []
|
||||
for (model_app_label, model_name), model_state in state.models.items():
|
||||
|
@ -276,7 +273,7 @@ class RenameModel(ModelOperation):
|
|||
changed_field = None
|
||||
remote_field = field.remote_field
|
||||
if remote_field:
|
||||
remote_model_tuple = self._get_model_tuple(
|
||||
remote_model_tuple = ModelTuple.from_model(
|
||||
remote_field.model, model_app_label, model_name
|
||||
)
|
||||
if remote_model_tuple == old_model_tuple:
|
||||
|
@ -284,7 +281,7 @@ class RenameModel(ModelOperation):
|
|||
changed_field.remote_field.model = new_remote_model
|
||||
through_model = getattr(remote_field, 'through', None)
|
||||
if through_model:
|
||||
through_model_tuple = self._get_model_tuple(
|
||||
through_model_tuple = ModelTuple.from_model(
|
||||
through_model, model_app_label, model_name
|
||||
)
|
||||
if through_model_tuple == old_model_tuple:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue