Skip to content
Browse files

Fixed #16317 -- Fixed dumpdata for self-referencing models and natura…

…l keys

Thanks aldaran for the patch.
  • Loading branch information...
1 parent 19a810b commit 4b722b31e1778310ba0c1ed96fdacf99aa203dd2 @claudep claudep committed Jun 24, 2012
View
4 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))
View
4 tests/regressiontests/fixtures_regress/fixtures/forward_ref_lookup.json
@@ -10,13 +10,15 @@
"pk": "2",
"model": "fixtures_regress.store",
"fields": {
+ "main": null,
"name": "Amazon"
}
},
{
"pk": "3",
"model": "fixtures_regress.store",
"fields": {
+ "main": null,
"name": "Borders"
}
},
@@ -29,4 +31,4 @@
"stores": [["Amazon"], ["Borders"]]
}
}
-]
+]
View
1 tests/regressiontests/fixtures_regress/models.py
@@ -91,6 +91,7 @@ def get_by_natural_key(self, key):
class Store(models.Model):
objects = TestManager()
name = models.CharField(max_length=255)
+ main = models.ForeignKey('self', null=True)
class Meta:
ordering = ('name',)
View
2 tests/regressiontests/fixtures_regress/tests.py
@@ -478,7 +478,7 @@ def test_nk_on_serialize(self):
)
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):

0 comments on commit 4b722b3

Please sign in to comment.
Something went wrong with that request. Please try again.