當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。