From 81dd5c247366986339c9c22c058e8b0864392cd1 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Wed, 9 Dec 2009 03:49:25 +0000 Subject: [PATCH] Fixed #12340 -- `BaseGenericInlineFormSet.get_queryset` now returns an ordered queryset. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11800 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/contenttypes/generic.py | 5 ++++- tests/regressiontests/generic_inline_admin/tests.py | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py index ac4e558023..b9b487bcf2 100644 --- a/django/contrib/contenttypes/generic.py +++ b/django/contrib/contenttypes/generic.py @@ -317,10 +317,13 @@ class BaseGenericInlineFormSet(BaseModelFormSet): from django.contrib.contenttypes.models import ContentType if self.instance is None or self.instance.pk is None: return self.model._default_manager.none() - return self.model._default_manager.filter(**{ + qs = self.model._default_manager.filter(**{ self.ct_field.name: ContentType.objects.get_for_model(self.instance), self.ct_fk_field.name: self.instance.pk, }) + if not qs.ordered: + qs = qs.order_by(self.model._meta.pk.name) + return qs def save_new(self, form, commit=True): # Avoid a circular import. diff --git a/tests/regressiontests/generic_inline_admin/tests.py b/tests/regressiontests/generic_inline_admin/tests.py index 60a370fb2d..d28cd16e1d 100644 --- a/tests/regressiontests/generic_inline_admin/tests.py +++ b/tests/regressiontests/generic_inline_admin/tests.py @@ -81,6 +81,11 @@ class GenericAdminViewTest(TestCase): inline_formset = generic_inlineformset_factory(Media, exclude=('url',)) + # Regression test for #12340. + e = Episode.objects.get(name='This Week in Django') + formset = inline_formset(instance=e) + self.failUnless(formset.get_queryset().ordered) + class GenericInlineAdminParametersTest(TestCase): fixtures = ['users.xml'] @@ -139,4 +144,4 @@ class GenericInlineAdminParametersTest(TestCase): e = self._create_object(EpisodeExclude) response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeexclude/%s/' % e.pk) formset = response.context['inline_admin_formsets'][0].formset - self.failIf('url' in formset.forms[0], 'The formset has excluded "url" field.') \ No newline at end of file + self.failIf('url' in formset.forms[0], 'The formset has excluded "url" field.')