GoobyPrs
GoobyPrs

Reputation: 615

Django dicitionary from formset errors

I have formset:

SomeFormSet = inlineformset_factory(Obj1, Obj2, fields='__all__', extra=1)
formset = SomeFormSet(request.POST or None, instance=instance)
if request.method == 'POST':
    if formset.is_valid():
        formset.save()
    else:
        #get_error_dictionary      

And I want to get dictionary from formset.errors like: number_of_form-field : error. How can I do that? Thanks

Upvotes: 0

Views: 88

Answers (1)

Curtis Olson
Curtis Olson

Reputation: 967

formset.errors will actually return a list of dictionaries. So for your case, you'll want a nested for loop. The following example will iterate over the list and each list's dictionary values.

for dict in formset.errors
    for error in dict.values
        error
    endfor
endfor         

OR

formset.non_form_errors

Upvotes: 1

Related Questions