当前位置: 首页>>代码示例>>Python>>正文


Python formsets.BaseFormSet方法代码示例

本文整理汇总了Python中django.forms.formsets.BaseFormSet方法的典型用法代码示例。如果您正苦于以下问题:Python formsets.BaseFormSet方法的具体用法?Python formsets.BaseFormSet怎么用?Python formsets.BaseFormSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.forms.formsets的用法示例。


在下文中一共展示了formsets.BaseFormSet方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_empty_formset_is_valid

# 需要导入模块: from django.forms import formsets [as 别名]
# 或者: from django.forms.formsets import BaseFormSet [as 别名]
def test_empty_formset_is_valid(self):
        """An empty formset still calls clean()"""
        class EmptyFsetWontValidate(BaseFormSet):
            def clean(self):
                raise ValidationError('Clean method called')

        EmptyFsetWontValidateFormset = formset_factory(FavoriteDrinkForm, extra=0, formset=EmptyFsetWontValidate)
        formset = EmptyFsetWontValidateFormset(
            data={'form-INITIAL_FORMS': '0', 'form-TOTAL_FORMS': '0'},
            prefix="form",
        )
        formset2 = EmptyFsetWontValidateFormset(
            data={'form-INITIAL_FORMS': '0', 'form-TOTAL_FORMS': '1', 'form-0-name': 'bah'},
            prefix="form",
        )
        self.assertFalse(formset.is_valid())
        self.assertFalse(formset2.is_valid()) 
开发者ID:nesdis,项目名称:djongo,代码行数:19,代码来源:test_formsets.py

示例2: test_form_kwargs_formset_dynamic

# 需要导入模块: from django.forms import formsets [as 别名]
# 或者: from django.forms.formsets import BaseFormSet [as 别名]
def test_form_kwargs_formset_dynamic(self):
        """Form kwargs can be passed dynamically in a formset."""
        class DynamicBaseFormSet(BaseFormSet):
            def get_form_kwargs(self, index):
                return {'custom_kwarg': index}

        DynamicFormSet = formset_factory(CustomKwargForm, formset=DynamicBaseFormSet, extra=2)
        formset = DynamicFormSet(form_kwargs={'custom_kwarg': 'ignored'})
        for i, form in enumerate(formset):
            self.assertTrue(hasattr(form, 'custom_kwarg'))
            self.assertEqual(form.custom_kwarg, i) 
开发者ID:nesdis,项目名称:djongo,代码行数:13,代码来源:test_formsets.py

示例3: test_formset_iteration

# 需要导入模块: from django.forms import formsets [as 别名]
# 或者: from django.forms.formsets import BaseFormSet [as 别名]
def test_formset_iteration(self):
        """Formset instances are iterable."""
        ChoiceFormset = formset_factory(Choice, extra=3)
        formset = ChoiceFormset()
        # An iterated formset yields formset.forms.
        forms = list(formset)
        self.assertEqual(forms, formset.forms)
        self.assertEqual(len(formset), len(forms))
        # A formset may be indexed to retrieve its forms.
        self.assertEqual(formset[0], forms[0])
        with self.assertRaises(IndexError):
            formset[3]

        # Formsets can override the default iteration order
        class BaseReverseFormSet(BaseFormSet):
            def __iter__(self):
                return reversed(self.forms)

            def __getitem__(self, idx):
                return super().__getitem__(len(self) - idx - 1)

        ReverseChoiceFormset = formset_factory(Choice, BaseReverseFormSet, extra=3)
        reverse_formset = ReverseChoiceFormset()
        # __iter__() modifies the rendering order.
        # Compare forms from "reverse" formset with forms from original formset
        self.assertEqual(str(reverse_formset[0]), str(forms[-1]))
        self.assertEqual(str(reverse_formset[1]), str(forms[-2]))
        self.assertEqual(len(reverse_formset), len(forms)) 
开发者ID:nesdis,项目名称:djongo,代码行数:30,代码来源:test_formsets.py

示例4: test_non_form_errors_run_full_clean

# 需要导入模块: from django.forms import formsets [as 别名]
# 或者: from django.forms.formsets import BaseFormSet [as 别名]
def test_non_form_errors_run_full_clean(self):
        """
        If non_form_errors() is called without calling is_valid() first,
        it should ensure that full_clean() is called.
        """
        class BaseCustomFormSet(BaseFormSet):
            def clean(self):
                raise ValidationError("This is a non-form error")

        ChoiceFormSet = formset_factory(Choice, formset=BaseCustomFormSet)
        formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
        self.assertIsInstance(formset.non_form_errors(), ErrorList)
        self.assertEqual(list(formset.non_form_errors()), ['This is a non-form error']) 
开发者ID:nesdis,项目名称:djongo,代码行数:15,代码来源:test_formsets.py

示例5: test_post_pallet_select_name_form

# 需要导入模块: from django.forms import formsets [as 别名]
# 或者: from django.forms.formsets import BaseFormSet [as 别名]
def test_post_pallet_select_name_form(self):

        user = create_user('alice', 'shrock')

        client = Client()
        client.force_login(user)

        # -------------------
        # No pallet selected
        # -------------------
        response = client.post(
            self.url,
            {
                'form_name': BuildPalletView.PALLET_SELECT_FORM_NAME,
            }
        )
        self.assertEqual(
            400,
            response.status_code,
            response.content.decode(),
        )
        self.assertIsInstance(
            response.context.get('pallet_select_form'),
            PalletSelectForm,
        )

        # -------------------------------------------------
        # Pallet selected
        # -------------------------------------------------
        pallet = Pallet.objects.create(name="test pallet")

        response = client.post(
            self.url,
            {
                'form_name': BuildPalletView.PALLET_SELECT_FORM_NAME,
                'pallet': pallet.pk
            }
        )
        self.assertEqual(200, response.status_code)

        build_pallet_form = response.context.get('form')
        box_forms = response.context.get('box_forms')
        pallet_form = response.context.get('pallet_form')

        self.assertIsInstance(build_pallet_form, BuildPalletForm)
        self.assertIsInstance(box_forms, BaseFormSet)
        self.assertIsInstance(pallet_form, HiddenPalletForm)

        pallet_form = response.context.get('pallet_form')
        self.assertIsInstance(pallet_form, HiddenPalletForm)

        pallet_from_context = pallet_form.initial.get('pallet')
        self.assertEqual(pallet, pallet_from_context)
        self.assertEqual(Pallet.FILL, pallet_from_context.pallet_status) 
开发者ID:w-a-r-m-inventory-system,项目名称:Food-Pantry-Inventory,代码行数:56,代码来源:test_views.py

示例6: test_post_pallet_name_form

# 需要导入模块: from django.forms import formsets [as 别名]
# 或者: from django.forms.formsets import BaseFormSet [as 别名]
def test_post_pallet_name_form(self):

        user = create_user('doug', 'state')

        client = Client()
        client.force_login(user)

        # -----------------------
        # No pallet name entered
        # -----------------------
        response = client.post(
            self.url,
            {
                'form_name': BuildPalletView.PALLET_NAME_FORM_NAME,
            }
        )
        self.assertEqual(400, response.status_code)
        self.assertIsInstance(
            response.context.get('pallet_name_form'),
            PalletNameForm,
        )

        # -------------------------------------------------
        # pallet name supplied
        # -------------------------------------------------
        pallet_name = "nuevo palet"
        response = client.post(
            self.url,
            {
                'form_name': BuildPalletView.PALLET_NAME_FORM_NAME,
                'name': pallet_name,
            }
        )
        self.assertEqual(200, response.status_code)

        build_pallet_form = response.context.get('form')
        box_forms = response.context.get('box_forms')
        pallet_form = response.context.get('pallet_form')

        self.assertIsInstance(build_pallet_form, BuildPalletForm)
        self.assertIsInstance(box_forms, BaseFormSet)
        self.assertIsInstance(pallet_form, HiddenPalletForm)

        pallet_form = response.context.get('pallet_form')
        self.assertIsInstance(pallet_form, HiddenPalletForm)

        pallet = pallet_form.initial.get('pallet')
        self.assertEqual(
            Pallet.objects.get(name=pallet_name),
            pallet,
        )
        self.assertEqual(Pallet.FILL, pallet.pallet_status) 
开发者ID:w-a-r-m-inventory-system,项目名称:Food-Pantry-Inventory,代码行数:54,代码来源:test_views.py


注:本文中的django.forms.formsets.BaseFormSet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。