2017-09-29 16 views
0

私の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で撮影
。それは私の理解から、私は理由filemultipartを投稿する必要があり、

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. 

しかし、このエラーで失敗しましたします。

質問:
それは同じ時間でJSONFieldFileFieldを持っているエンドポイントを撮影unittestのやることは可能ですか?

参考:パーサをいじった後
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 14: invalid start byte

+0

ファイルを文字列にデコードし、そのオブジェクトをformat = jsonとして送信できますか?ファイルには何が入っていますか? –

+0

は右のパーサーが使用されているので、ここではdiscusedとしています。https://stackoverflow.com/questions/36881771/django-rest-framework-file-upload-causing-an-unsupported-media-type-multipart? – andi

+0

@SamRedway私は決してそれをしていません。お返事をありがとうございます。これは古くからの質問ですが、解決策を見つけることができませんでした。 – Sarit

答えて

0


設定に何も間違っていることがわかりました。私が忘れたのは一つのことだけです。私は単引用符を入れなければならない{"author": "Singh"}をカバーする必要があります。
ウェブブラウザは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) 
関連する問題