My
models.py
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
After played around with parser.
I found that nothing in the configurations are wrong. Only one thing I missed is. I have to put single quote cover the {"author": "Singh"}
.
Because web browser does submit in the str
not python object.
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)