简体   繁体   English

Django REST UnitTest 没有提交文件

[英]Django REST UnitTest No file was submitted

I successfully implement with small cases.我成功地实施了小案例。 Then I started to work with bigger structure.然后我开始使用更大的结构。 And I got the error.我得到了错误。
Error:错误:
No file was submitted.

import tempfile
from unittest import skip

from django.conf import settings
from django.contrib.auth.models import User
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from model_mommy import mommy
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase, APIClient


class CustomerFromExcelViewsetTest(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.soken_staff = mommy.make(User, username='spearhead')
        self.user = mommy.make(User, username='Justin')
        settings.MEDIA_ROOT = tempfile.mkdtemp()

    def test_upload_file(self):
        """Expect created_user, and updated_user correct set"""
        file = File(open('./soken_web/apps/uploaded_files/complete-customer.xlsx', 'rb'))
        uploaded_file = SimpleUploadedFile('new_excel.xlsx', file.read(), content_type='multipart/form-data')
        data = {
            file: uploaded_file,
        }
        self.client.force_authenticate(user=self.user)
        response = self.client.post(reverse('api:customer_from_excel-list'), data, format='multipart')
        response.render()

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)

Here they are the models , serializers , and viewsets它们是modelsserializersviewsets

models.py模型.py
https://gist.github.com/elcolie/52daf2bd144af82b348f7353656be434 https://gist.github.com/elcolie/52daf2bd144af82b348f7353656be434

serializers.py序列化程序.py
https://gist.github.com/elcolie/7f097642c4a752e76044c6938c49e097 https://gist.github.com/elcolie/7f097642c4a752e76044c6938c49e097

viewsets.py视图集.py
https://gist.github.com/elcolie/34fa66632209f14624899d997919d3fb https://gist.github.com/elcolie/34fa66632209f14624899d997919d3fb

After a day I could not figure out where is the that bug.一天后,我无法弄清楚那个错误在哪里。

References:参考:
DRF APITestCase not use `multipart` with other param DRF APITestCase 不将 `multipart` 与其他参数一起使用

looks like you missed quotes in data dict.看起来你错过了data字典中的引号。 It should be:它应该是:

data = {
    'file': uploaded_file,
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试发布POST时未提交文件。 Django Rest Framework - No File was submitted while trying to make a POST. Django Rest Framework Django REST框架上传图片时出错,提交的数据不是文件 - Django rest framework error while uploading image, submitted data is not file django rest framework 3 ImageField发送ajax结果“没有提交文件。” - django rest framework 3 ImageField send ajax result “No file was submitted.” Django REST Framework上传图片:“提交的数据不是文件” - Django REST Framework upload image: “The submitted data was not a file” 如何在Django Rest Framework单元测试中模拟日志记录? - How to mock logging in Django Rest Framework unittest? 如何使用 XML 对 Django Rest 框架端点进行单元测试? - How to unittest Django Rest Framework endpoint with XML? 在 Django 中使用 unittest 对 REST API 进行功能测试 - Feature-testing a REST API with unittest in Django 当尝试使用django Rest框架和REACT与Axios上传文件时,如何修复“没有提交文件。” - How to fix “No file was submitted.” when trying to upload a file with django Rest framework and REACT with Axios Django Rest Framework+React JS,无法实现表单解析器(错误:提交的数据不是文件。检查表单上的编码类型。) - Django Rest Framework+React JS ,unable to implement form parser (error: The submitted data was not a file. Check the encoding type on the form.) django-rest-framework- 接受提交的外键字段的 id - django-rest-framework- accepting id for submitted foreign key field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM