尝试发布POST时未提交文件。 Django Rest Framework

时间:2017-03-29 19:11:13

标签: python django django-rest-framework

我正在尝试制作“POST”,但问题是我收到了“没有文件提交错误”,我不知道为什么。

在我的 models.py

我在 PersonalDetail

中没有“icon”

PersonalDetailSample

function displayPicker(round){
    for (var i = 0; i <= round; i++){
      document.write('<img src=img/die' + i + '.png id="' + 'picker' + i + '" class="img">');
    }
    var elems = document.getElementsByClassName('img');
        Array.from(elems).forEach((v,i) => v.addEventListener('click', function() {
          console.log(`You picked ${i}`);
        }));
}

displayPicker(5);

serializers.py

class PersonalDetailSample(models.Model): 
    ...
    icon = models.FileField(_('icon'), db_index=True, null=True, blank=True, upload_to='files/')
    ...

并在我的 views.py

class PersonalDetailSerializer(serializers.ModelSerializer):
    ...
    icon = serializers.FileField(source='details_sample.icon')
    ...

    class Meta:
        model = PersonalDetail
        fields = (
            ..., 'icon', ...
        )

我想做的帖子:

def perform_create(self, serializer):
    preview_text, preview_name = personal_details.find_text_name(serializer.validated_data)
    serializer.save(user_id=self.request.user,
                    details_sample=serializer.validated_data['details_sample'],
                    content=serializer.validated_data['content'],
                    preview_name=preview_name)

我收到此错误:(400 Bad Request)

  

{     “图标”:[       “没有提交文件。”     ]   }

我不知道为什么......

也许有人遇到同样的麻烦。

1 个答案:

答案 0 :(得分:3)

尝试在序列化程序上使用required = False,或者如果您不需要使用rest api使用read_only = True在文件上完全写入。

必需=错误选项http://www.django-rest-framework.org/api-guide/fields/#required

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="marquee">
  <div class="marquee-wrapper">
    <div class="marquee-content">
      Updates: Update (8 Mar 2016): Now plugin have new option: startVisible The marquee will be visible in the start if set to true. Thanks to @nuke-ellington 👠Update (24 Jan 2014): Note: people who been asking me how to use this plugin with content being
      loaded with Ajax, please read notes about this update. New methods added, so now after you start the plugin using var $mq = $('.marquee').marquee();, you start the plugin using var $mq = $('.marquee').marquee();, you start the plugin using var $mq
      = $('.marquee').marquee();, then you can pause, resume, togglepause, resume) and desestroy destroy toggle(pause, resume) and destroy toggle(pause, resume) and destroy methods e.g to remove the marquee plugin from your element simply use $mq.marquee('destroy');.
      Similarly you can use pause the marquee any time using $mq.marquee('pause');.
    </div>
  </div>
</section>

OR read_only选项 http://www.django-rest-framework.org/api-guide/fields/#read_only

class PersonalDetailSerializer(serializers.ModelSerializer):
    ...
    icon = serializers.FileField(source='details_sample.icon', required=False)
    ...

    class Meta:
        model = PersonalDetail
        fields = (
            ..., 'icon', ...
        )