예제 #1
0
class BadParentFormExistingField(NestedFormMixin, forms.Form):
    nested_form_configs = [
        NestedFormConfig(
            cls=NestedForm1,
            key='nested_form_1',
            pre=True,
        )
    ]

    # Duplicate form field from nested
    nested_form_1 = forms.CharField(
        error_messages={'required': 'Three is required'})

    def save(self):  # pragma: no cover
        pass
예제 #2
0
class ParentForm(NestedFormMixin, forms.Form):
    nested_form_configs = [
        NestedFormConfig(cls=NestedForm1,
                         key='nested_form_1',
                         required=False,
                         required_key='nested_form_1_required',
                         pre=True,
                         error_messages={
                             'three': {
                                 'required': 'Nested three is required'
                             },
                             'bad': {
                                 'required': 'Not required'
                             }
                         }),
        NestedFormConfig(
            cls=NestedForm2,
            key='nested_form_2',
            required=False,
            required_key='nested_form_2_required',
            pre=True,
        ),
        NestedFormConfig(
            cls=OptionalForm,
            key='optional',
            required=False,
            field_prefix='optional_1',
            required_key='optional_required',
            pre=True,
        ),
        NestedFormConfig(
            cls=OptionalForm,
            key='optional_2',
            required=False,
            field_prefix='optional_2',
            required_key='optional_required_2',
            post=True,
        )
    ]

    one = forms.CharField(error_messages={'required': 'One is required'})
    two = forms.CharField(required=False)

    def get_nested_form_init_args(self, nested_form_config, args, kwargs,
                                  base_form_args, base_form_kwargs):
        if nested_form_config.key == 'nested_form_1':
            args = [1] + args
        elif nested_form_config.key == 'nested_form_2':
            args = [2] + args

        # Return the updated args
        return args, kwargs

    def get_nested_form_save_args(self, nested_form_config, args, kwargs,
                                  base_form_args, base_form_kwargs,
                                  form_responses):
        if nested_form_config.key == 'nested_form_1':
            kwargs.update({
                'number': 10,
            })
        elif nested_form_config.key == 'nested_form_2':
            kwargs.update({
                'user_id': 5,
            })

        # Return the updated args
        return args, kwargs

    def save(self, *args, **kwargs):
        return 'the object'
예제 #3
0
 class ParentForm1(NestedFormMixin, forms.Form):
     nested_form_configs = [
         NestedFormConfig(cls=Form1, key='one'),
         NestedFormConfig(cls=Form1, key='two')
     ]