Skip to content

Commit

Permalink
Fixed #16317 -- Fixed dumpdata for self-referencing models and natura…
Browse files Browse the repository at this point in the history
…l keys

Thanks aldaran for the patch.
  • Loading branch information
claudep committed Jun 24, 2012
1 parent 19a810b commit 4b722b3
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions django/core/management/commands/dumpdata.py
Expand Up @@ -150,11 +150,11 @@ def sort_dependencies(app_list):
for field in model._meta.fields: for field in model._meta.fields:
if hasattr(field.rel, 'to'): if hasattr(field.rel, 'to'):
rel_model = 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) deps.append(rel_model)
for field in model._meta.many_to_many: for field in model._meta.many_to_many:
rel_model = 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) deps.append(rel_model)
model_dependencies.append((model, deps)) model_dependencies.append((model, deps))


Expand Down
Expand Up @@ -10,13 +10,15 @@
"pk": "2", "pk": "2",
"model": "fixtures_regress.store", "model": "fixtures_regress.store",
"fields": { "fields": {
"main": null,
"name": "Amazon" "name": "Amazon"
} }
}, },
{ {
"pk": "3", "pk": "3",
"model": "fixtures_regress.store", "model": "fixtures_regress.store",
"fields": { "fields": {
"main": null,
"name": "Borders" "name": "Borders"
} }
}, },
Expand All @@ -29,4 +31,4 @@
"stores": [["Amazon"], ["Borders"]] "stores": [["Amazon"], ["Borders"]]
} }
} }
] ]
1 change: 1 addition & 0 deletions tests/regressiontests/fixtures_regress/models.py
Expand Up @@ -91,6 +91,7 @@ def get_by_natural_key(self, key):
class Store(models.Model): class Store(models.Model):
objects = TestManager() objects = TestManager()
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
main = models.ForeignKey('self', null=True)


class Meta: class Meta:
ordering = ('name',) ordering = ('name',)
Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/fixtures_regress/tests.py
Expand Up @@ -478,7 +478,7 @@ def test_nk_on_serialize(self):
) )
self.assertEqual( self.assertEqual(
stdout.getvalue(), 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): def test_dependency_sorting(self):
Expand Down

0 comments on commit 4b722b3

Please sign in to comment.