Provide a hook for compound form/formset validation in ModelAdmin
|Reported by:||mrts||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||no|
Sometimes it is necessary to validate inline formset data based on the main form data and vice-versa.
See e.g. http://groups.google.com/group/django-users/browse_thread/thread/259c1679ddaaceb8/6ad3698e5451d3fe for a use case.
My use case is somewhat similar: an object can be published only if its associated items meet certain criteria. Thus, to validate the "is published" flag in the form, access to formsets is required.
For advanced users a simple change is sufficient: substitute
if all_valid(formsets) and form_validated: with
if self.formsets_are_valid(formsets, form, form_validated) and form_validated: in
def formset_are_valid(self, formsets, ...): return all_valid(formsets). The latter can then be overriden for arbitrary further manipulations. Will upload a patch eventually.
However, this means that people have to mess with either
FormSet internals directly (
_non_form_errors etc) to expose the errors.
This is yet another proof of a conceptual problem, quoting myself from http://groups.google.com/group/django-developers/browse_thread/thread/f9aae709a7fda689/fb16a17ab6a31931 :
"Inline formsets usually represent
composition, i.e. the inline objects are inseparable from the
main entity. The relation is stronger than a ForeignKey (from the
main entity to another entity), yet the latter is displayed as a
field of the form and can be easily manipulated -- but not the inlines."
So, this is yet another reason for introducing FormsetFields in the long run --
Form.clean() would have access to formsets both in the admin and ordinary forms.
Change History (11)
comment:1 Changed 7 years ago by
|Patch needs improvement:||unset|