Ticket #17478: 17478-1.diff

File 17478-1.diff, 3.7 KB (added by Claude Paroz, 11 years ago)
  • django/forms/formsets.py

    diff --git a/django/forms/formsets.py b/django/forms/formsets.py
    index fd98c43..e3a729b 100644
    a b from django.forms.fields import IntegerField, BooleanField  
    66from django.forms.util import ErrorList
    77from django.forms.widgets import Media, HiddenInput
    88from django.utils.encoding import python_2_unicode_compatible
     9from django.utils.functional import cached_property
    910from django.utils.safestring import mark_safe
    1011from django.utils import six
    1112from django.utils.six.moves import xrange
    class BaseFormSet(object):  
    5556        self.error_class = error_class
    5657        self._errors = None
    5758        self._non_form_errors = None
    58         # construct the forms in the formset
    59         self._construct_forms()
    6059
    6160    def __str__(self):
    6261        return self.as_table()
    class BaseFormSet(object):  
    122121            initial_forms = len(self.initial) if self.initial else 0
    123122        return initial_forms
    124123
    125     def _construct_forms(self):
    126         # instantiate all the forms and put them in self.forms
    127         self.forms = []
     124    @cached_property
     125    def forms(self):
     126        """
     127        Instantiate forms at first property access.
     128        """
    128129        # DoS protection is included in total_form_count()
    129         for i in xrange(self.total_form_count()):
    130             self.forms.append(self._construct_form(i))
     130        forms = [self._construct_form(i) for i in xrange(self.total_form_count())]
     131        return forms
    131132
    132133    def _construct_form(self, i, **kwargs):
    133134        """
  • tests/forms_tests/tests/test_formsets.py

    diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py
    index 1e9e7db3..eca3f7d 100644
    a b ArticleFormSet = formset_factory(ArticleForm)  
    10701070
    10711071class TestIsBoundBehavior(TestCase):
    10721072    def test_no_data_raises_validation_error(self):
    1073         self.assertRaises(ValidationError, ArticleFormSet, {})
     1073        with self.assertRaises(ValidationError):
     1074            ArticleFormSet({}).is_valid()
    10741075
    10751076    def test_with_management_data_attrs_work_fine(self):
    10761077        data = {
  • tests/model_formsets/tests.py

    diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py
    index 03cd3b0..4a8b363 100644
    a b from decimal import Decimal  
    88from django import forms
    99from django.db import models
    1010from django.forms.models import (_get_foreign_key, inlineformset_factory,
    11     modelformset_factory)
     11    modelformset_factory, BaseModelFormSet)
    1212from django.test import TestCase, skipUnlessDBFeature
    1313from django.utils import six
    1414
    class ModelFormsetTest(TestCase):  
    386386        formset = PostFormSet()
    387387        self.assertFalse("subtitle" in formset.forms[0].fields)
    388388
     389    def test_custom_queryset_init(self):
     390        """
     391        Test that a queryset can be overriden in the __init__ method.
     392        https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset
     393        """
     394        author1 = Author.objects.create(name='Charles Baudelaire')
     395        author2 = Author.objects.create(name='Paul Verlaine')
     396
     397        class BaseAuthorFormSet(BaseModelFormSet):
     398            def __init__(self, *args, **kwargs):
     399                super(BaseAuthorFormSet, self).__init__(*args, **kwargs)
     400                self.queryset = Author.objects.filter(name__startswith='Charles')
     401
     402        AuthorFormSet = modelformset_factory(Author, formset=BaseAuthorFormSet)
     403        formset = AuthorFormSet()
     404        self.assertEqual(len(formset.get_queryset()), 1)
     405
    389406    def test_model_inheritance(self):
    390407        BetterAuthorFormSet = modelformset_factory(BetterAuthor, fields="__all__")
    391408        formset = BetterAuthorFormSet()
Back to Top