Fixed 17478 -- Allowed queryset overriding in BaseModelFormSet init

BaseModelFormSet.forms is now a cached property instead of being
populated in the __init__ method. This behaviour also matches an
example in the documentation.
Thanks Thomasz Swiderski for the report and Simon Charette for the
review.
This commit is contained in:
Claude Paroz 2013-06-22 09:25:14 +02:00
parent 9be93aa809
commit ef79582e86
3 changed files with 28 additions and 9 deletions

View file

@ -8,7 +8,7 @@ from decimal import Decimal
from django import forms
from django.db import models
from django.forms.models import (_get_foreign_key, inlineformset_factory,
modelformset_factory)
modelformset_factory, BaseModelFormSet)
from django.test import TestCase, skipUnlessDBFeature
from django.utils import six
@ -386,6 +386,23 @@ class ModelFormsetTest(TestCase):
formset = PostFormSet()
self.assertFalse("subtitle" in formset.forms[0].fields)
def test_custom_queryset_init(self):
"""
Test that a queryset can be overriden in the __init__ method.
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset
"""
author1 = Author.objects.create(name='Charles Baudelaire')
author2 = Author.objects.create(name='Paul Verlaine')
class BaseAuthorFormSet(BaseModelFormSet):
def __init__(self, *args, **kwargs):
super(BaseAuthorFormSet, self).__init__(*args, **kwargs)
self.queryset = Author.objects.filter(name__startswith='Charles')
AuthorFormSet = modelformset_factory(Author, formset=BaseAuthorFormSet)
formset = AuthorFormSet()
self.assertEqual(len(formset.get_queryset()), 1)
def test_model_inheritance(self):
BetterAuthorFormSet = modelformset_factory(BetterAuthor, fields="__all__")
formset = BetterAuthorFormSet()