Django: How to save a formset with a custom model form? -
trying save bunch of objects custom form:
class customform(forms.modelform): class meta: model = widget complexify = models.booleanfield()
when complexify checked, need complex operations on widget object.
i can't do:
for object in formset.save(commit=false): ...
because won't have complexify
flag.
and going through each form seems wrong way:
for form in formset.forms: ...
because includes (empty) forms , deleted forms.
any ideas on how done?
the best answer find problem overriding save
on form:
class customform(forms.modelform): class meta: model = widget complexify = models.booleanfield() def save(self, *args, **kwargs): obj = super(customform, self).save(*args, **kwargs) obj.complexify = self.cleaned_data.get("complexify") return obj
then available when handle them:
for object in formset.save(commit=false): if object.complexify: object.do_complicated()
Comments
Post a Comment