B
B
blackbb2018-09-06 15:05:24
Django
blackbb, 2018-09-06 15:05:24

How to pass multichoicefiled values ​​to initial?

The search form has a multiple choice field. It is necessary that the form, when clicking on the search, does not dump the previously entered values, for this, with the usual fields (select), I do this

if 'type_of_container' in request.GET and request.GET['type_of_container'] != '':
        type_of_container = get_object_or_404(Typeofcontainer, id=request.GET['type_of_container'])
    else:
        type_of_container = None

form = CityChoiceForm(initial={
        'type_of_container':type_of_container,
        })

If a multiple choice field is done in the same way, it saves the last selected checkbox. That's why I do it
if 'type_of_body' in request.GET and request.GET.getlist('type_of_body') != '':
         type_of_body  = get_list_or_404(Typeofbody, id__in=request.GET.getlist('type_of_body'))
    else:
        type_of_body = None
form = CityChoiceForm(initial={
        'type_of_body':type_of_body,
        })

but in the form an empty value is obtained.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question