views:

19

answers:

2

I've declared a formset like so:

class BaseFeatureFormSet(BaseFormSet):
    def save(self, commit = True):
        feature = Feature(name = self.cleaned_data['name'], 
                     type = self.cleaned_data['type'], 
                     premium = self.cleaned_data['premium'],)
        feature.save()
        return feature


FeaturesFormset = formset_factory(EditFeatureForm, 
                                  formset = BaseFeatureFormSet, extra = 0)

So when I'm saving the formset, I'm getting a TypeError: list indices must be integers, not str referring to the first line of the save() function. How do I solve this error?

Update 1 Managed to solve this first bit thanks to gruszyczy. I'm not getting another TypeError: 'EditFeatureFormFormSet' object is not iterable from the next line in the code section:

for feature in features:
    feature.save()
    feature = vehicle.features.add(feature)

The error is from for feature in features: Ideas?

A: 

cleaned_data in this example is a list of form values. You have to iterate over it and inside you will find data you need:

for values in self.cleaned_data:
    feature = Feature(name=values['name'], ..

That's because formset is a list of forms, that are displayed and returns a list of form values. This is a simple concept to grasp, when you simple realise, that FormSet <-> [Form, Form, Form, ..]

gruszczy
This works....but then the next line in my code is giving another TypeError...let me edit my question
Stephen
Used the same method, but eliminated the save() function and instead did a for feature in features.cleaned_data loop within the view. Thanks for the idea
Stephen
Glad I could help :-)
gruszczy
A: 

Where exactly is features defined as a list/tuple?

domino
I think you have a point there...as the save() function will always save the first item no matter how many features are there in the formset. I'm stuck here
Stephen