Fixed #16317 -- Fixed dumpdata for self-referencing models and natural keys

Thanks aldaran for the patch.
This commit is contained in:
Claude Paroz 2012-06-24 19:54:56 +02:00
parent 19a810b18c
commit 4b722b31e1
4 changed files with 7 additions and 4 deletions

View File

@ -150,11 +150,11 @@ def sort_dependencies(app_list):
for field in model._meta.fields:
if hasattr(field.rel, 'to'):
rel_model = field.rel.to
if hasattr(rel_model, 'natural_key'):
if hasattr(rel_model, 'natural_key') and rel_model != model:
deps.append(rel_model)
for field in model._meta.many_to_many:
rel_model = field.rel.to
if hasattr(rel_model, 'natural_key'):
if hasattr(rel_model, 'natural_key') and rel_model != model:
deps.append(rel_model)
model_dependencies.append((model, deps))

View File

@ -10,6 +10,7 @@
"pk": "2",
"model": "fixtures_regress.store",
"fields": {
"main": null,
"name": "Amazon"
}
},
@ -17,6 +18,7 @@
"pk": "3",
"model": "fixtures_regress.store",
"fields": {
"main": null,
"name": "Borders"
}
},
@ -29,4 +31,4 @@
"stores": [["Amazon"], ["Borders"]]
}
}
]
]

View File

@ -91,6 +91,7 @@ class TestManager(models.Manager):
class Store(models.Model):
objects = TestManager()
name = models.CharField(max_length=255)
main = models.ForeignKey('self', null=True)
class Meta:
ordering = ('name',)

View File

@ -478,7 +478,7 @@ class NaturalKeyFixtureTests(TestCase):
)
self.assertEqual(
stdout.getvalue(),
"""[{"pk": 2, "model": "fixtures_regress.store", "fields": {"name": "Amazon"}}, {"pk": 3, "model": "fixtures_regress.store", "fields": {"name": "Borders"}}, {"pk": 4, "model": "fixtures_regress.person", "fields": {"name": "Neal Stephenson"}}, {"pk": 1, "model": "fixtures_regress.book", "fields": {"stores": [["Amazon"], ["Borders"]], "name": "Cryptonomicon", "author": ["Neal Stephenson"]}}]"""
"""[{"pk": 2, "model": "fixtures_regress.store", "fields": {"main": null, "name": "Amazon"}}, {"pk": 3, "model": "fixtures_regress.store", "fields": {"main": null, "name": "Borders"}}, {"pk": 4, "model": "fixtures_regress.person", "fields": {"name": "Neal Stephenson"}}, {"pk": 1, "model": "fixtures_regress.book", "fields": {"stores": [["Amazon"], ["Borders"]], "name": "Cryptonomicon", "author": ["Neal Stephenson"]}}]"""
)
def test_dependency_sorting(self):