我的models.py
有3个字段。其中之一是JSONField()
attribute = JSONField(null=True, blank=True) # Free to add any note to here
type = models.CharField(max_length=30, choices=FileType.choices, default=FileType.zipcode)
file = models.FileField(upload_to='import_files')
为了方便起见,我通常设置JSONField(null=True, blank=True)
。
def test_upload_and_process_data_complete_case(self):
from soken_web.apps.imported_files.models import ImportFile
with open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx', 'rb') as uploaded_file:
data = {
'attribute': {'author': 'Singh'},
'type': ImportFile.FileType.zipcode,
'file': uploaded_file
}
response = self.client.post(reverse('api:import_file-list'), data, format='multipart')
response.render()
self.assertEqual(status.HTTP_201_CREATED, response.status_code)
我的测试运行良好,没有使用JSONField
实验:
当我像JSONField
那样拍摄时。它会因此错误而失败
AssertionError: Test data contained a dictionary value for key 'attribute', but multipart uploads do not support nested data. You may want to consider using format='json' in this test case.
但是,根据我的理解,由于multipart
,我必须使用file
发帖。
问题:
是否有可能在同一时间内进行单一测试拍摄JSONField
和FileField
的端点?
参考:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 14: invalid start byte
答案 0 :(得分:0)
解决了解析器之后
我发现配置中没有任何错误。我唯一遗漏的是。我必须把单引号覆盖{"author": "Singh"}
因为Web浏览器确实在str
而不是python对象中提交。
def test_upload_and_process_data_complete_case(self):
from soken_web.apps.imported_files.models import ImportFile
with open(str(settings.BASE_DIR) + '/apps/zipcodes/complete.xlsx', 'rb') as uploaded_file:
data = {
'attribute': '{"author": "Singh"}',
'type': ImportFile.FileType.zipcode,
'file': uploaded_file
}
response = self.client.post(reverse('api:import_file-list'), data, format='multipart')
response.render()
self.assertEqual(status.HTTP_201_CREATED, response.status_code)