Is it possible to specifyc validate_min and validate_max on contrib.Admin formset ? (From Django's code, it seems not)

56 views
Skip to first unread message

boito...@gmail.com

unread,
Aug 12, 2015, 6:01:03 AM8/12/15
to Django users
Hi,

 I would need to enforce the max_num and min_num  attributes of formsets on my admin pages at validation time.
From the documentation and the source, it seems that validate_max and validate_min would be the FormSet attributes that need to set to True for that to happen.

Yet, while investigating this issue, it seems to me that it is currently not possible to forward them to FromSet classes used in the Admin site:
1) From InlineModelAdmin.get_formset() code, it is not useful to set validate_* on the inline itself, as it would not be forwarded to the inlineformset_factory() call on return.
2) From formset_factory() code, it is not useful to set validate_* on the FormSet class itself, because it would be overwritten by the argument provided to the factory function.

Are you aware of a way to have the Admin instantiate FormSet classes with valide_min and validate_max set to true ?
If not, it seems to me it would be easy to allow it by extending the defaults dictionary in 1), so I am curious if there is a rationale for not allowing it, or if it is a simple oversight ?

Thank you very much for reading,
  Ad

boito...@gmail.com

unread,
Aug 17, 2015, 4:07:08 AM8/17/15
to Django users
There is actually a workflow that feels inconsistent if we cannot easily enforce validate_min & _max on the Formset of the contrib.Admin.
Example:
a) For model A, attach an Inline for model B (B having a ForeingKey to A), with min_num < max_num
b) create an instance of A in the Admin interface: you will need to create between min_num and max_num B instance in the formset (because min_num and max_num are checked on creation)
c) edit the inserted A instance, you can delete as many B attached to it as you want, allowing to save the result with number of B < min_num (<- this is what validate_min would prevent)
d) now if you open the modified A instance for edition, you cannot save it anymore even if you do not change anything (because you have less B than min_num)
Reply all
Reply to author
Forward
0 new messages