[3.1.x] Fixed #31742 -- Fixed makemigrations crash on ForeignKey to an app with mixed case label.

Regression in 9e1b6b8a66.

Thanks Ignacio Santolin for the report.
Backport of 62d85a2835 from master
This commit is contained in:
Mariusz Felisiak 2020-06-26 23:18:59 +02:00
parent 1a3029b6b4
commit 5263480d7f
2 changed files with 33 additions and 1 deletions

View File

@ -582,6 +582,10 @@ class ForeignObject(RelatedField):
if self.remote_field.parent_link:
kwargs['parent_link'] = self.remote_field.parent_link
if isinstance(self.remote_field.model, str):
if '.' in self.remote_field.model:
app_label, model_name = self.remote_field.model.split('.')
kwargs['to'] = '%s.%s' % (app_label, model_name.lower())
else:
kwargs['to'] = self.remote_field.model.lower()
else:
kwargs['to'] = self.remote_field.model._meta.label_lower

View File

@ -867,6 +867,34 @@ class StateTests(SimpleTestCase):
with self.assertRaisesMessage(ValueError, msg):
project_state.apps
def test_reference_mixed_case_app_label(self):
new_apps = Apps()
class Author(models.Model):
class Meta:
app_label = 'MiXedCase_migrations'
apps = new_apps
class Book(models.Model):
author = models.ForeignKey(Author, models.CASCADE)
class Meta:
app_label = 'MiXedCase_migrations'
apps = new_apps
class Magazine(models.Model):
authors = models.ManyToManyField(Author)
class Meta:
app_label = 'MiXedCase_migrations'
apps = new_apps
project_state = ProjectState()
project_state.add_model(ModelState.from_model(Author))
project_state.add_model(ModelState.from_model(Book))
project_state.add_model(ModelState.from_model(Magazine))
self.assertEqual(len(project_state.apps.get_models()), 3)
def test_real_apps(self):
"""
Including real apps can resolve dangling FK errors.