Skip to content

cannot have a composite form when forms have fields with the same names #3

@zsoldosp

Description

@zsoldosp

Below is the sample code to illustrate. I picked the well-known online shop example, focusing on the checkout step, when billing and delivery addresses are specified.

>>> from composite_form.forms import CompositeForm
>>> from django import forms
>>> class Address(forms.Form):
...     address = forms.CharField('address details')
...
>>> class BillingAddress(Address):
...     def __init__(self, *args, **kwargs):
...         kwargs['prefix'] = kwargs.get('prefix', 'billing')
...         super(BillingAddress, self).__init__(*args, **kwargs)
...
>>> class DeliveryAddress(Address):
...     def __init__(self, *args, **kwargs):
...         kwargs['prefix'] = kwargs.get('prefix', 'delivery')
...         super(DeliveryAddress, self).__init__(*args, **kwargs)
...
>>> class CheckoutAddressForm(CompositeForm):
...     form_list = [BillingAddress, DeliveryAddress]
...
>>> checkout_form = CheckoutAddressForm(data={'billing-address': 'Billing Address details', 'delivery-address': 'Delivery Address details'})
>>> checkout_form.is_valid()
True
>>> checkout_form.cleaned_data
{'address': u'Delivery Address details'}
>>> # I would have expected two addresses with prefixes there or that __init__ failed with an error telling me same field names are not supported

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions