t0ster / django-composite-form Goto Github PK
View Code? Open in Web Editor NEWHelper class to handle form composition
Helper class to handle form composition
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
if instance is None a previous kwargs dictionary is used in next iteration.
Affected code (forms.CompositeForm.__init__
):
for form in self.form_list:
if self.get_form_instance(form):
kwargs.update({"instance": self.get_form_instance(form)})
self._form_instances[form] = form(data, files, *args, **kwargs)
a possible fix:
for form in self.form_list:
kwargs = initkwargs.copy()
if self.get_form_instance(form):
kwargs.update({"instance": self.get_form_instance(form)})
self._form_instances[form] = form(data, files, *args, **kwargs)
CompositeForms looks interesting. Please provide some docs. Thank you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.