diff options
| author | Anton I. Sipos <aisipos@gmail.com> | 2012-11-04 15:42:17 -0800 |
|---|---|---|
| committer | Anton I. Sipos <aisipos@gmail.com> | 2012-11-04 15:52:05 -0800 |
| commit | e44ab5bb4fd3aa826ca4243a8ea9fd7125800da2 (patch) | |
| tree | 8882072ce524d389d2f41aec78835e9037fbc6b2 /tests | |
| parent | 8d3f932f18c75fcbb32cf3dd5998445d65b5db0f (diff) | |
Fixed #18949 -- Improve performance of model_to_dict with many-to-many
When calling model_to_dict, improve performance of the generated SQL by
using values_list to determine primary keys of many to many objects. Add
a specific test for this function, test_model_to_dict_many_to_many
Thanks to brian for the original report and suggested fix.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/model_forms/tests.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py index 947d0cf3c3..46d5af565a 100644 --- a/tests/modeltests/model_forms/tests.py +++ b/tests/modeltests/model_forms/tests.py @@ -561,6 +561,42 @@ class UniqueTest(TestCase): "slug": "Django 1.0"}, instance=p) self.assertTrue(form.is_valid()) +class ModelToDictTests(TestCase): + """ + Tests for forms.models.model_to_dict + """ + def test_model_to_dict_many_to_many(self): + categories=[ + Category(name='TestName1', slug='TestName1', url='url1'), + Category(name='TestName2', slug='TestName2', url='url2'), + Category(name='TestName3', slug='TestName3', url='url3') + ] + for c in categories: + c.save() + writer = Writer(name='Test writer') + writer.save() + + art = Article( + headline='Test article', + slug='test-article', + pub_date=datetime.date(1988, 1, 4), + writer=writer, + article='Hello.' + ) + art.save() + for c in categories: + art.categories.add(c) + art.save() + + with self.assertNumQueries(1): + d = model_to_dict(art) + + #Ensure all many-to-many categories appear in model_to_dict + for c in categories: + self.assertIn(c.pk, d['categories']) + #Ensure many-to-many relation appears as a list + self.assertIsInstance(d['categories'], list) + class OldFormForXTests(TestCase): def test_base_form(self): self.assertEqual(Category.objects.count(), 0) |
