Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No File was submitted while trying to make a POST. Django Rest Framework

I'm trying to make a "POST", but the problem is that I get a "no file was submitted error" and I don't know why.

in my models.py

I don't have "icon" in PersonalDetail

PersonalDetailSample

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

serializers.py

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

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

and in my views.py

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)

The post I would like to do:

{
    "checked":true,
    "content": {
        "text_field_1": {
            "size":"small",
            "text":"My userdefined Title",
            "title":"Title"
        },
        "text_field_2": {
            "size":"big",
            "text":"I don't have inspiration",
            "title":"Nachricht"
        }
    },
    "details_sample":27,
    "preview_user_title": "This is my User Title"
}

And I get this error: (400 Bad Request)

{ "icon": [ "No file was submitted." ] }

And I don't know why...

Maybe someone had the same trouble.

like image 291
Chuck Aguilar Avatar asked Mar 29 '17 19:03

Chuck Aguilar


1 Answers

Try using required=False on serializer, or if you don't need to write at all on the file using the rest api use read_only=True.

required=False option http://www.django-rest-framework.org/api-guide/fields/#required

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

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

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

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

    class Meta:
        model = PersonalDetail
        fields = (
            ..., 'icon', ...
        )
like image 132
educolo Avatar answered Nov 05 '22 03:11

educolo