I have 2 radio buttons in the UI, one for setting a variable (ng-model) say
true (ng-value=true) and other one for setting it as
false (ng-value=false). Now, when none of them is selected it results in the variable
selected being absent from the outgoing request (as expected).
However, when that is dealt with Django Forms,
the self.data dictionary in the
clean() method gives
False on accessing
self.data['selected'] why is that so? Shouldn’t it be
None or at least give a
key-error when it was not even present in the actual request?
Note that the variable ‘selected’ is actually a field in a Django Model with
default=False, is that thing responsible for this behaviour? How can I circumvent this situation considering that altering the Django Model field isn’t an option?
So I dealt with it the other day by checking for the
selected key in the raw
request.body. Now, since its a string, I had to parse it to a dict and then access the mentioned key using :
In this way, if
selected is not present at all when none of the radio buttons are selected, I get
None. Similarly, if the radio button for
ng-value=true is selected then I get
True and vice-versa.
Answered By – Neeraj Kumar
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0