mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
Added tests/patch for AlterField when migrating an unmanaged model relating to ticket-35813.
Included unmanaged_keys to _prepare_field_lists logic for ticket-35813. Updated code formatting for ticket-35813.
This commit is contained in:
parent
c334c1a8ff
commit
ee280879be
@ -239,14 +239,14 @@ class MigrationAutodetector:
|
|||||||
self.through_users = {}
|
self.through_users = {}
|
||||||
self.old_field_keys = {
|
self.old_field_keys = {
|
||||||
(app_label, model_name, field_name)
|
(app_label, model_name, field_name)
|
||||||
for app_label, model_name in self.kept_model_keys
|
for app_label, model_name in self.kept_model_keys | self.kept_unmanaged_keys
|
||||||
for field_name in self.from_state.models[
|
for field_name in self.from_state.models[
|
||||||
app_label, self.renamed_models.get((app_label, model_name), model_name)
|
app_label, self.renamed_models.get((app_label, model_name), model_name)
|
||||||
].fields
|
].fields
|
||||||
}
|
}
|
||||||
self.new_field_keys = {
|
self.new_field_keys = {
|
||||||
(app_label, model_name, field_name)
|
(app_label, model_name, field_name)
|
||||||
for app_label, model_name in self.kept_model_keys
|
for app_label, model_name in self.kept_model_keys | self.kept_unmanaged_keys
|
||||||
for field_name in self.to_state.models[app_label, model_name].fields
|
for field_name in self.to_state.models[app_label, model_name].fields
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -712,6 +712,26 @@ class AutodetectorTests(BaseAutodetectorTests):
|
|||||||
author_unmanaged = ModelState(
|
author_unmanaged = ModelState(
|
||||||
"testapp", "AuthorUnmanaged", [], {"managed": False}, ("testapp.author",)
|
"testapp", "AuthorUnmanaged", [], {"managed": False}, ("testapp.author",)
|
||||||
)
|
)
|
||||||
|
author_unmanaged_name_default = ModelState(
|
||||||
|
"testapp",
|
||||||
|
"Author",
|
||||||
|
[
|
||||||
|
("id", models.AutoField(primary_key=True)),
|
||||||
|
("name", models.CharField(max_length=200, default="Ada Lovelace")),
|
||||||
|
],
|
||||||
|
{"managed": False},
|
||||||
|
("testapp.author",),
|
||||||
|
)
|
||||||
|
author_unmanaged_name_longer = ModelState(
|
||||||
|
"testapp",
|
||||||
|
"Author",
|
||||||
|
[
|
||||||
|
("id", models.AutoField(primary_key=True)),
|
||||||
|
("name", models.CharField(max_length=400)),
|
||||||
|
],
|
||||||
|
{"managed": False},
|
||||||
|
("testapp.author",),
|
||||||
|
)
|
||||||
author_unmanaged_managed = ModelState(
|
author_unmanaged_managed = ModelState(
|
||||||
"testapp", "AuthorUnmanaged", [], {}, ("testapp.author",)
|
"testapp", "AuthorUnmanaged", [], {}, ("testapp.author",)
|
||||||
)
|
)
|
||||||
@ -3520,7 +3540,7 @@ class AutodetectorTests(BaseAutodetectorTests):
|
|||||||
self.assertEqual(fk_field.remote_field.model, "testapp.AAuthorProxyProxy")
|
self.assertEqual(fk_field.remote_field.model, "testapp.AAuthorProxyProxy")
|
||||||
|
|
||||||
def test_unmanaged_create(self):
|
def test_unmanaged_create(self):
|
||||||
"""The autodetector correctly deals with managed models."""
|
"""The autodetector correctly deals with unmanaged models."""
|
||||||
# First, we test adding an unmanaged model
|
# First, we test adding an unmanaged model
|
||||||
changes = self.get_changes(
|
changes = self.get_changes(
|
||||||
[self.author_empty], [self.author_empty, self.author_unmanaged]
|
[self.author_empty], [self.author_empty, self.author_unmanaged]
|
||||||
@ -3532,6 +3552,18 @@ class AutodetectorTests(BaseAutodetectorTests):
|
|||||||
changes, "testapp", 0, 0, name="AuthorUnmanaged", options={"managed": False}
|
changes, "testapp", 0, 0, name="AuthorUnmanaged", options={"managed": False}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_unmanaged_alter_field(self):
|
||||||
|
"""Tests autodetection of new fields on an unmanaged model."""
|
||||||
|
changes = self.get_changes(
|
||||||
|
[self.author_unmanaged_name_default], [self.author_unmanaged_name_longer]
|
||||||
|
)
|
||||||
|
# Right number/type of migrations?
|
||||||
|
self.assertNumberMigrations(changes, "testapp", 1)
|
||||||
|
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
|
||||||
|
self.assertOperationAttributes(
|
||||||
|
changes, "testapp", 0, 0, name="name", preserve_default=True
|
||||||
|
)
|
||||||
|
|
||||||
def test_unmanaged_delete(self):
|
def test_unmanaged_delete(self):
|
||||||
changes = self.get_changes(
|
changes = self.get_changes(
|
||||||
[self.author_empty, self.author_unmanaged], [self.author_empty]
|
[self.author_empty, self.author_unmanaged], [self.author_empty]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user