Im writing a custom field/widget to display multiple input fields for related data, for example my product has 4 search fields, search1, search2, search3, etc.. , instead of having to define each field in my form, i want to have one field that will display as many input fields as i need ( all related data ) based on its length, here's what i have so far
class RelatedCategoryField(forms.MultiValueField):
"""
Custom field to display multiple input boxes for a related object
"""
def __init__(self, max_length, sub_max_length, label):
# sub_max_length, is the max_length of each subfield
self.total = max_length/sub_max_length
self.widget = CategoryWidget(self.total, label)
fields = ()
for num in range(self.total):
fields += (forms.CharField(label="%s-%s" %(label, num),
max_length=sub_max_length),)
super(RelatedCategoryField, self).__init__(fields, required=False)
def compress(self, value_list):
if value_list:
return value_list
return [[] for i in self.total]
class CategoryWidget(forms.MultiWidget):
"""
Custom widget
"""
def __init__(self, count, label):
self.count = count
self.label = label
widgets = [forms.TextInput(attrs={}) for sub in range(self.count)]
super(CategoryWidget, self).__init__(widgets)
def decompress(self, value):
if value:
return value
return [None for i in range(self.count)]
def format_output(self, rendered_widgets):
"""
Customize widget rendering
"""
return render_to_string('fields/categoryfield.html', {'fields': rendered_widgets})
so basically i call this field like so:
category = RelatedCategoryField(max_length=200, sub_max_length50, label="search")
then based on sub_max_length
the field determines how many fields it will create for this multivalue field and then the field label will be label+field# ( search_1, search_2, etc.. )
the above code is working fine, but my problem is that when displayed, the field only shows the label provided when the field was defined, and then it shows the input fields, i want to show each input field with its corresponding label, so to summarize my question, is it possible to display the label per field inside the multivalue field ?