我正在将我的Django应用程序从1.x迁移到2.2,运行单元测试时,我遇到了一个关于将None作为数据进行发布的错误。在先前的版本中允许发布None吗?是否可以通过RequestFactory发布None?
我不想提供一个空字符串,因为需要验证该字段。
r = RequestFactory()
rr = r.post("some-url",{"name":"sri", "kd_ratio":None})
错误:
File "/usr/local/lib/python3.7/site-packages/django/test/client.py", line 354, in post
post_data = self._encode_data(data, content_type)
File "/usr/local/lib/python3.7/site-packages/django/test/client.py", line 313, in _encode_data
return encode_multipart(BOUNDARY, data)
File "/usr/local/lib/python3.7/site-packages/django/test/client.py", line 197, in encode_multipart
'Cannot encode None as POST data. Did you mean to pass an '
TypeError: Cannot encode None as POST data. Did you mean to pass an empty string or omit the value?
format='json'
参数:r.post('some-url', {'name': 'sri', 'kd_ratio': None}, format='json')
- Masood Khaari