mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #22659 -- Prevent model states from sharing field instances.
Thanks to Trac alias tbartelmess for the report and the test project.
This commit is contained in:
parent
f384b638e2
commit
7a38f88922
3 changed files with 34 additions and 11 deletions
|
@ -356,7 +356,7 @@ class StateTests(TestCase):
|
|||
project_state = ProjectState()
|
||||
project_state.add_model_state(ModelState.from_model(TestModel))
|
||||
with self.assertRaises(ValueError):
|
||||
rendered_state = project_state.render()
|
||||
project_state.render()
|
||||
|
||||
# If we include the real app it should succeed
|
||||
project_state = ProjectState(real_apps=["contenttypes"])
|
||||
|
@ -372,3 +372,20 @@ class ModelStateTests(TestCase):
|
|||
def test_custom_model_base(self):
|
||||
state = ModelState.from_model(ModelWithCustomBase)
|
||||
self.assertEqual(state.bases, (models.Model,))
|
||||
|
||||
def test_bound_field_sanity_check(self):
|
||||
field = models.CharField(max_length=1)
|
||||
field.model = models.Model
|
||||
with self.assertRaisesMessage(ValueError,
|
||||
'ModelState.fields cannot be bound to a model - "field" is.'):
|
||||
ModelState('app', 'Model', [('field', field)])
|
||||
|
||||
def test_fields_immutability(self):
|
||||
"""
|
||||
Tests that rendering a model state doesn't alter its internal fields.
|
||||
"""
|
||||
apps = Apps()
|
||||
field = models.CharField(max_length=1)
|
||||
state = ModelState('app', 'Model', [('name', field)])
|
||||
Model = state.render(apps)
|
||||
self.assertNotEqual(Model._meta.get_field('name'), field)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue