<legend id='32siU'><style id='32siU'><dir id='32siU'><q id='32siU'></q></dir></style></legend>

<small id='32siU'></small><noframes id='32siU'>

      <tfoot id='32siU'></tfoot>
        <bdo id='32siU'></bdo><ul id='32siU'></ul>

    1. <i id='32siU'><tr id='32siU'><dt id='32siU'><q id='32siU'><span id='32siU'><b id='32siU'><form id='32siU'><ins id='32siU'></ins><ul id='32siU'></ul><sub id='32siU'></sub></form><legend id='32siU'></legend><bdo id='32siU'><pre id='32siU'><center id='32siU'></center></pre></bdo></b><th id='32siU'></th></span></q></dt></tr></i><div id='32siU'><tfoot id='32siU'></tfoot><dl id='32siU'><fieldset id='32siU'></fieldset></dl></div>

      Django REST 框架 + Django REST Swagger + ImageField

      Django REST Framework + Django REST Swagger + ImageField(Django REST 框架 + Django REST Swagger + ImageField)

      1. <small id='kxwQW'></small><noframes id='kxwQW'>

          <tbody id='kxwQW'></tbody>
        <i id='kxwQW'><tr id='kxwQW'><dt id='kxwQW'><q id='kxwQW'><span id='kxwQW'><b id='kxwQW'><form id='kxwQW'><ins id='kxwQW'></ins><ul id='kxwQW'></ul><sub id='kxwQW'></sub></form><legend id='kxwQW'></legend><bdo id='kxwQW'><pre id='kxwQW'><center id='kxwQW'></center></pre></bdo></b><th id='kxwQW'></th></span></q></dt></tr></i><div id='kxwQW'><tfoot id='kxwQW'></tfoot><dl id='kxwQW'><fieldset id='kxwQW'></fieldset></dl></div>

        <legend id='kxwQW'><style id='kxwQW'><dir id='kxwQW'><q id='kxwQW'></q></dir></style></legend>
            <tfoot id='kxwQW'></tfoot>

              <bdo id='kxwQW'></bdo><ul id='kxwQW'></ul>
              1. 本文介绍了Django REST 框架 + Django REST Swagger + ImageField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我创建了一个带有 ImageField 的简单模型,我想使用 django-rest-framework + django-rest-swagger 创建一个 api 视图,该视图已记录并能够上传文件.

                I created a simple Model with an ImageField and I wanna make an api view with django-rest-framework + django-rest-swagger, that is documented and is able to upload the file.

                这是我得到的:

                models.py

                from django.utils import timezone
                from django.db import models
                
                class MyModel(models.Model):
                
                    source = models.ImageField(upload_to=u'/photos')
                    is_active = models.BooleanField(default=False)
                    created_at = models.DateTimeField(default=timezone.now)
                
                    def __unicode__(self):
                        return u"photo {0}".format(self.source.url)
                

                serializer.py

                from .models import MyModel
                
                class MyModelSerializer(serializers.ModelSerializer):
                
                    class Meta:
                        model = MyModel
                        fields = [
                            'id',
                            'source',
                            'created_at',
                        ]
                

                views.py

                from rest_framework import generics
                from .serializer import MyModelSerializer
                
                class MyModelView(generics.CreateAPIView):
                    serializer_class = MyModelSerializer
                    parser_classes = (FileUploadParser, )
                
                    def post(self, *args, **kwargs):
                        """
                            Create a MyModel
                            ---
                            parameters:
                                - name: source
                                  description: file
                                  required: True
                                  type: file
                            responseMessages:
                                - code: 201
                                  message: Created
                        """
                        return super(MyModelView, self).post(self, *args, **kwargs)
                

                urls.py

                from weddings.api.views import MyModelView
                
                urlpatterns = patterns(
                    '',
                    url(r'^/api/mymodel/$', MyModelView.as_view()),
                )
                

                对我来说,这应该很简单.但是,我无法使上传工作.我总是得到这个错误响应:

                For me this should be pretty simple. However, I can't make the upload work. I always get this error response:

                我已经从 django-rest 阅读了这部分文档-框架:

                如果与 FileUploadParser 一起使用的视图使用文件名 URL 关键字参数调用,则该参数将用作文件名.如果在没有文件名 URL 关键字参数的情况下调用它,则客户端必须在 Content-Disposition HTTP 标头中设置文件名.例如 Content-Disposition:附件;文件名=upload.jpg.

                但是,标题是由 django-rest-swagger 在 Request Payload 属性中传递的(来自 chrome 控制台).

                However the Header is being passed by django-rest-swagger in the Request Payload property (from chrome console).

                如果需要更多信息,请告诉我.

                If any more info is necessary, please let me know.

                我正在使用 Django==1.8.8djangorestframework==3.3.2django-rest-swagger==0.3.4.

                I'm using Django==1.8.8, djangorestframework==3.3.2 and django-rest-swagger==0.3.4.

                推荐答案

                这是我想出的最终解决方案:

                This is the final solution I came up with:

                from rest_framework import generics
                from rest_framework.parsers import FormParser, MultiPartParser
                from .serializer import MyModelSerializer
                
                class MyModelView(generics.CreateAPIView):
                    serializer_class = MyModelSerializer
                    parser_classes = (FormParser, MultiPartParser)
                
                    def post(self, *args, **kwargs):
                        """
                            Create a MyModel
                            ---
                            parameters:
                                - name: source
                                  description: file
                                  required: True
                                  type: file
                            responseMessages:
                                - code: 201
                                  message: Created
                        """
                        return super(MyModelView, self).post(self, *args, **kwargs)
                

                我所要做的就是将解析器从 FileUploadParser 更改为 (FormParser, MultiPartParser)

                All I had to do was change the parsers from FileUploadParser to (FormParser, MultiPartParser)

                这篇关于Django REST 框架 + Django REST Swagger + ImageField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                    <tbody id='zL0rb'></tbody>

                  <tfoot id='zL0rb'></tfoot><legend id='zL0rb'><style id='zL0rb'><dir id='zL0rb'><q id='zL0rb'></q></dir></style></legend>
                  <i id='zL0rb'><tr id='zL0rb'><dt id='zL0rb'><q id='zL0rb'><span id='zL0rb'><b id='zL0rb'><form id='zL0rb'><ins id='zL0rb'></ins><ul id='zL0rb'></ul><sub id='zL0rb'></sub></form><legend id='zL0rb'></legend><bdo id='zL0rb'><pre id='zL0rb'><center id='zL0rb'></center></pre></bdo></b><th id='zL0rb'></th></span></q></dt></tr></i><div id='zL0rb'><tfoot id='zL0rb'></tfoot><dl id='zL0rb'><fieldset id='zL0rb'></fieldset></dl></div>

                    • <bdo id='zL0rb'></bdo><ul id='zL0rb'></ul>

                      <small id='zL0rb'></small><noframes id='zL0rb'>