[PYTHON] Talking about array parameter transfer to django's QueryDict

I'm writing a program that forwards the parameters received by the Django service B from ʻA to another service C` as they are, without any modification.

In one pattern, if the parameter was an array, there was a bug where only the last element was transferred to service C, not the entire array.

Source before modification:

>>> qd = QueryDict('egg=1&egg=2&p=3')
>>> response = requests.get('http://localhost:12345/', params=qd)

The billing URL for the service C is now /? P = 3 \ u0026egg = 2.

Upon examination, there is a big difference between QueryDict and Python dict. If QueryDict is passed as-is as dict, all parameters will be one value. It seems that the QueryDict.get () method is used inside.

>>> qd.get('egg')
u'2'
>>> qd.getlist('egg')
[u'1', u'2']

QueryDict has adict ()method, but it behaves differently than expected.

>>> qd.dict()
{u'p': u'3', u'egg': u'2'}

Finally, I found this method.

>>> dict(qd.iterlists())
{u'p': [u'3'], u'egg': [u'1', u'2']}

The modified program looks like this:

>>> response = requests.get('http://localhost:12345/', params=dict(qd.iterlists()))

This time the service C was correctly transferred as /? P = 3 \ u0026egg = 1 \ u0026egg = 2, and ʻegg` was transferred in the form of an array.

Recommended Posts

Talking about array parameter transfer to django's QueryDict
A memorandum of understanding about django's QueryDict
About Django's ProxyModel