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.
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', ...
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With