Multiple forms in one set of Django

In my model, I have a Condition class and two subclasses of StringCondition and NumCondition. The user will view / edit / order conditions as one list.

Is there a way to implement this using the Django model form set? I am currently using two separate forms (one for NumConditions, one for StringConditions), but it is a pain to manually interlace the ordering (so that a number condition can appear between two string conditions, etc.).

+3
source share
1 answer

Starting with Django 1.3, you can override the __iter__()form set method to control the order in which forms are displayed. To use this function, you need to do two things: first mark your forms so that you can arrange them, and then render the formet as for form in formset(instead of the old for form in formset.forms). Then create a method __iter__()to order forms and you are in business.

Kent

+2
source

Source: https://habr.com/ru/post/1776721/


All Articles