mirror of
https://github.com/django/django.git
synced 2025-08-03 10:34:04 +00:00
Refs #24215 -- Improved error message for unhandled lazy model operations.
This commit is contained in:
parent
617eff41ac
commit
25c157e4cc
2 changed files with 70 additions and 7 deletions
|
@ -625,8 +625,16 @@ class StateTests(SimpleTestCase):
|
|||
app_label = "migrations"
|
||||
apps = new_apps
|
||||
|
||||
class Publisher(models.Model):
|
||||
name = models.TextField()
|
||||
|
||||
class Meta:
|
||||
app_label = "migrations"
|
||||
apps = new_apps
|
||||
|
||||
class Book(models.Model):
|
||||
author = models.ForeignKey(Author, models.CASCADE)
|
||||
publisher = models.ForeignKey(Publisher, models.CASCADE)
|
||||
|
||||
class Meta:
|
||||
app_label = "migrations"
|
||||
|
@ -642,20 +650,42 @@ class StateTests(SimpleTestCase):
|
|||
# Make a valid ProjectState and render it
|
||||
project_state = ProjectState()
|
||||
project_state.add_model(ModelState.from_model(Author))
|
||||
project_state.add_model(ModelState.from_model(Publisher))
|
||||
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)
|
||||
self.assertEqual(len(project_state.apps.get_models()), 4)
|
||||
|
||||
# now make an invalid one with a ForeignKey
|
||||
project_state = ProjectState()
|
||||
project_state.add_model(ModelState.from_model(Book))
|
||||
with self.assertRaises(ValueError):
|
||||
msg = (
|
||||
"Unhandled pending operations for models:\n"
|
||||
" migrations.author (referred to by fields: migrations.Book.author)\n"
|
||||
" migrations.publisher (referred to by fields: migrations.Book.publisher)"
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
project_state.apps
|
||||
|
||||
# and another with ManyToManyField
|
||||
# And another with ManyToManyField.
|
||||
project_state = ProjectState()
|
||||
project_state.add_model(ModelState.from_model(Magazine))
|
||||
with self.assertRaises(ValueError):
|
||||
msg = (
|
||||
"Unhandled pending operations for models:\n"
|
||||
" migrations.author (referred to by fields: "
|
||||
"migrations.Magazine.authors, migrations.Magazine_authors.author)"
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
project_state.apps
|
||||
|
||||
# And now with multiple models and multiple fields.
|
||||
project_state.add_model(ModelState.from_model(Book))
|
||||
msg = (
|
||||
"Unhandled pending operations for models:\n"
|
||||
" migrations.author (referred to by fields: migrations.Book.author, "
|
||||
"migrations.Magazine.authors, migrations.Magazine_authors.author)\n"
|
||||
" migrations.publisher (referred to by fields: migrations.Book.publisher)"
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
project_state.apps
|
||||
|
||||
def test_real_apps(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue