【问题标题】:how to post an image file in the django rest framework如何在 django rest 框架中发布图像文件
【发布时间】:2015-05-02 20:49:44
【问题描述】:

我正在对我的其余框架 API、我的模型进行单元测试:

class attach():
    attachment = models.ImageField
    name = models.CharField

然后我尝试通过我定义的 API 发布(此处未显示标准序列化程序)

c = Client()
with open('wishlist.doc') as fp:
     c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp}, format='multipart')

我得到以下堆栈跟踪

ValueError: The 'attachment' attribute has no file associated with it.

如何将密钥附件与文件关联

【问题讨论】:

    标签: django-rest-framework python-unittest


    【解决方案1】:

    确保在创建请求时设置format="multipart"

    [...]
    c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp}, format='multipart')
    

    来源:https://stackoverflow.com/a/27576436/1682844

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 2017-12-25
      • 2019-07-08
      • 2021-12-09
      • 2015-06-18
      • 2021-09-02
      • 2018-10-30
      相关资源
      最近更新 更多