You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’m trying to create a form with dynamically created fields, adding those fields to field sets depending on some conditions. By inspecting the library code I was trying like this:
class BuildCreateForm(BetterForm):
description = forms.CharField(label = 'Description')
def __init__(self, *args, **kwargs):
product = kwargs.pop('product')
super(BuildCreateForm, self).__init__(*args, **kwargs)
for field in product.fields.all():
field_id = 'field_%s' % field.pk
self.fields[field_id] = forms.CharField(
label = field.name,
required = False)
self.Meta.fieldsets[0][1]['fields'].append(field_id)
class Meta:
fieldsets = [('build_fields', {
'fields': [],
'legend': 'Fields'})]
which obviously fails, as adding something to Meta multiple times will be reflected in all instances (ie. refreshing my test page will display one more of the dynamic field after each refresh). As Fieldset has no method to add a field, I was wondering if it is actually possible.
Thanks in advance!
Best,
Gergely
The text was updated successfully, but these errors were encountered:
It's certainly possible, but it's not public API. The metaclass copies the fieldsets from the Meta inner class to the base_fieldsets class attribute on class creation, and then in BetterForm.__init__ the base_fieldsets are copied to the self._fieldsets instance attribute. So it's the latter you'd want to modify in order to tweak just the one form instance.
Hello,
I’m trying to create a form with dynamically created fields, adding those fields to field sets depending on some conditions. By inspecting the library code I was trying like this:
which obviously fails, as adding something to
Meta
multiple times will be reflected in all instances (ie. refreshing my test page will display one more of the dynamic field after each refresh). AsFieldset
has no method to add a field, I was wondering if it is actually possible.Thanks in advance!
Best,
Gergely
The text was updated successfully, but these errors were encountered: