1

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!!

Community
  • 1
  • 1
bytejunkie
  • 1,003
  • 14
  • 30

2 Answers2

1

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 .

bytejunkie
  • 1,003
  • 14
  • 30
0

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.

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
  • I've tried to do it this way, but it doesn't work for the dynamic fields wher the value is wiped out when its initialised with a query set later. I have successfully re-populated the same form where it exists on its own rather than as part of a formset by passing a dict of initial data. i just need to know why my code which is similar to the article linked isn't working. – bytejunkie Aug 20 '12 at 15:59
  • Are you just trying to supply an initial value for the "supplier" field? or? – Brandon Taylor Aug 20 '12 at 16:01
  • ive got a dictionary of fields to fill. basically, im building a bulk add feature. user visits and fills out the template form, then the view will refill the template form with the choices and also build a formset filled with the choices and some additional options. – bytejunkie Aug 20 '12 at 19:39
  • Have you taken a look at: https://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-initial-data-with-a-formset ? – Brandon Taylor Aug 20 '12 at 20:06