Question

as per this SO question Django Passing Custom Form Parameters to Formset im tryin to use curry to pass a dictionary of initial values to my formset.

its not working, it gives me a formset of empty values, which isn't what its supposed to do.

can anyone see if im implementing this wrong?

data = {'supplier': input_data['supplier'],}

InstanceFormSet = formset_factory(BulkAddInstanceForm, extra=int(input_data['copies']))
InstanceFormSet.form = staticmethod(curry(BulkAddInstanceForm, data))

EDIT: as requested

class BulkAddInstanceForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(BulkAddInstanceForm, self).__init__(*args, **kwargs)

        self.fields['supplier'] = forms.ModelChoiceField(
            queryset=Supplier.objects.all(),
            label='Supplier',
            empty_label='Select a Supplier...',
            required=True,
        )
        self.fields['syllabus'] = forms.ModelChoiceField(
            queryset=Syllabus.objects.all(),
            label='Syllabus',
            empty_label='Select a Syllabus...',
            widget=FilterWidget(
                queryset=Syllabus.objects.all(),
                parent_queryset=Supplier.objects.all(),
                control_name='supplier',
                parent_attr='supplier',
            ),
            required=False,
        )
        self.fields['venue'] = forms.ModelChoiceField(
            queryset=Venue.objects.all(),
            label='Venue',
            empty_label='Select a Venue...',
            widget=FilterWidget(
                queryset=Venue.objects.all(),
                parent_queryset=Supplier.objects.all(),
                control_name='supplier',
                parent_attr='supplier',
            ),
            required=False,
        )
    start_date = NiceDateField(required=False, label='Start Date')
    residential = forms.BooleanField(label='Res?', required=False)
    special_offers = forms.BooleanField(label='S/O?', required=False)
    manual_price = forms.IntegerField(required=False)
    manual_cost = forms.IntegerField(required=False)

edit2: FAO Brandon

I have looked at the docco and it suggests to do a different thing.

formset = formset_factory(BulkAddInstanceForm, extra=int(input_data['copies']))
formset = InstanceFormSet(initial=[data, ])

which creates a formset with 'copies' amount of forms, plus another one with the data in. if i do this

formset = InstanceFormSet(initial=[data, data])

then i get two extra forms with the data in. so my idea is i need an iterator to add 'copies' number of dictionaries and zero the initial number of forms in the formset.

not that i know how to cod that yet!!

Was it helpful?

Solution

managed to do it with the following code:

InstanceFormSet = formset_factory(BulkAddInstanceForm, extra=0)
# build the list for populating the forms
    n, datalist = 0, []
    while n < int(input_data['copies']):
        datalist.append(data)
        print datalist
        n +=1

    formset = InstanceFormSet(initial=datalist)

so i create the formset, then i create a list of the data dictionaries, then populate the formset with the inital data as a list.

seems to be working so far, though i've got to work out how to collect and submit the info yet .

OTHER TIPS

You would need to pick up the value you're passing in during the __init__

When I've used this in the past, I've set up the parameter as such:

class MyForm(forms.Form):
    def __init__(self, data, *args, **kwargs):
        self.data = data
        #do whatever else I need to do with data

        super(MyForm, self).__init__(*args, **kwargs)

Hope that helps you out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top