第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

DeliciousPie:是否可以在批量請求中發(fā)布多個文件

DeliciousPie:是否可以在批量請求中發(fā)布多個文件

慕尼黑8549860 2021-03-12 14:08:06
我想發(fā)表大量帖子。問題在于每個項目都需要一張圖像(甚至可能只有幾張)。是否可以通過批量請求做到這一點?該模型:class CollageItem(models.Model):  url = models.URLField(null = True)  image = models.FileField(upload_to = 'i')  thumbnail = models.FileField(upload_to = 't')和TastyPie對象:class CollageItemResource(ModelResource):  image = fields.FileField(attribute = 'image', null = True, blank = true)  thumbnail = fields.FileField(attribute = 'thumbnail', null = True, blank = true)  class Meta:    queryset = CollageItem.objects.all(    resource_name = "collage_item"我可以使用批量請求發(fā)布多張圖片,還是應該回復到單個帖子?
查看完整描述

2 回答

?
白衣染霜花

TA貢獻1796條經(jīng)驗 獲得超10個贊

我走了自定義序列化器之路:


class FormPostSerializer(Serializer):

    formats = ['form']

    content_types = {

        'form': 'multipart/form-data',

    }


    def from_form(self, content):

        try:

            dict = cgi.parse_multipart(StringIO(content), self.form_boundary)

        except Exception, e:

            raise e

        for key, value in dict.iteritems():

            dict[key] = value[0] if len(value) > 0 else None

        return dict

并且是需要發(fā)布多個文件的所有資源的基類:


class FormResource(ModelResource):

    class Meta:

        serializer = FormPostSerializer()


    def dispatch(self, request_type, request, **kwargs):

        cth = request.META.get('CONTENT_TYPE') or \

            request.META.get('Content-type') or \

            self._meta.serializer.content_types['json']

        self.Meta.serializer.form_boundary = self.parse_content_type_header(cth)

        return super(FormResource, self).dispatch(request_type, request, **kwargs)


    def parse_content_type_header(self, content_type_header):

        parts = cgi.parse_header(content_type_header)

        rv = {}

        for p in parts:

            if isinstance(p, dict):

                rv = dict(rv.items() + p.items())

        return rv

當然,序列化程序需要一些其他處理(例如,UTF8字段),我從答案中省略了這些處理。


查看完整回答
反對 回復 2021-03-30
?
MYYA

TA貢獻1868條經(jīng)驗 獲得超4個贊

當然可以!根據(jù)圖像的大小,您必須決定上載它們是否花費太長時間,但是有可能。根據(jù)好吃的文檔,可以通過Patch選項進行批量創(chuàng)建和更新。



查看完整回答
反對 回復 2021-03-30
  • 2 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號