Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logic behind Form(request.POST or None)

Tags:

python

django

What's the logic behind request.POST or None? I haven't seen such thing in Python projects except Django.

Since or operator returns True or False values, how is it possible that if request.POST isn't None, the Form knows it and takes post as an argument?

form = MyModelForm(request.POST or None)

In fact, the result should be Form(True) if request.POST isn't None, otherwise Form(False).

How it works?

like image 608
Milano Avatar asked Jul 07 '16 17:07

Milano


1 Answers

The use of or in this case does not evaluate to True or False, but returns one of the objects.

Keep in mind that or is evaluated from left to right.

When the QueryDict request.POST is empty, it takes a Falsy value, so the item on RHS of the or operation is selected (which is None), and the form is initialized without vanilla arguments (i.e. with None):

form = MyModelForm()

Otherwise, when request.POST is not empty, the form is initialized with the QueryDict:

form = MyModelForm(request.POST)
like image 95
Moses Koledoye Avatar answered Nov 14 '22 14:11

Moses Koledoye