私のmodels.py
には3つのフィールドがあります。そのうちの一つは、私は通常やすさのためにJSONField(null=True, blank=True)
を設定JSONField()
テストアップロードExcelファイルとJSONField Django REST
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')
です。
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
で撮影。それは私の理解から、私は理由file
のmultipart
を投稿する必要があり、
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.
しかし、このエラーで失敗しましたします。
質問:
それは同じ時間でJSONField
とFileField
を持っているエンドポイントを撮影unittestのやることは可能ですか?
参考:パーサをいじった後
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 14: invalid start byte
ファイルを文字列にデコードし、そのオブジェクトをformat = jsonとして送信できますか?ファイルには何が入っていますか? –
は右のパーサーが使用されているので、ここではdiscusedとしています。https://stackoverflow.com/questions/36881771/django-rest-framework-file-upload-causing-an-unsupported-media-type-multipart? – andi
@SamRedway私は決してそれをしていません。お返事をありがとうございます。これは古くからの質問ですが、解決策を見つけることができませんでした。 – Sarit