Reputation: 6339
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?
Upvotes: 2
Views: 339
Reputation: 42208
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, ..]
Upvotes: 2