From 4b722b31e1778310ba0c1ed96fdacf99aa203dd2 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sun, 24 Jun 2012 19:54:56 +0200 Subject: [PATCH] Fixed #16317 -- Fixed dumpdata for self-referencing models and natural keys Thanks aldaran for the patch. --- django/core/management/commands/dumpdata.py | 4 ++-- .../fixtures_regress/fixtures/forward_ref_lookup.json | 4 +++- tests/regressiontests/fixtures_regress/models.py | 1 + tests/regressiontests/fixtures_regress/tests.py | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index 9059625dec..d3650b1eb8 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -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)) diff --git a/tests/regressiontests/fixtures_regress/fixtures/forward_ref_lookup.json b/tests/regressiontests/fixtures_regress/fixtures/forward_ref_lookup.json index fe50c653cc..42e8ec0877 100644 --- a/tests/regressiontests/fixtures_regress/fixtures/forward_ref_lookup.json +++ b/tests/regressiontests/fixtures_regress/fixtures/forward_ref_lookup.json @@ -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"]] } } -] \ No newline at end of file +] diff --git a/tests/regressiontests/fixtures_regress/models.py b/tests/regressiontests/fixtures_regress/models.py index 5d23a21dcd..14cf880003 100644 --- a/tests/regressiontests/fixtures_regress/models.py +++ b/tests/regressiontests/fixtures_regress/models.py @@ -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',) diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py index 405c566826..ab93341699 100644 --- a/tests/regressiontests/fixtures_regress/tests.py +++ b/tests/regressiontests/fixtures_regress/tests.py @@ -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):