You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to make the ImageField as required in Swagger POST API #66
I'm using the same serializer setup and mine seems to be working fine. Maybe this is an issue of differences in versions. Here are the versions of Django, Django Extra Fields, Django REST Framework and Django REST Swagger that I have used, so you can compare: Django==2.1.7, django-extra-fields==1.0.0, djangorestframework==3.9.2, django-rest-swagger==2.2.0.
I wonder if it's because of the readOnly property? Would a readOnly property make it optional because it's not required when writing to the API? Is this because of how the file is normally returned as a URL rather than the data directly? (I'm just spit balling at this point).
ReadOnly fields cannot be required at the same time, therefore it's not possible to have a field where both required and read_only are set to True in the latest version of Django REST Framework.
What's weird is in my case, I never set read_only to True. The serializer is a WritableNestedSerializer, but I don't see how that would affect the field.
class ImageSerializer(WritableNestedModelSerializer):
image = Base64ImageField(required=True)
class Meta:
model = Image
fields = ('id', 'name', 'image')
I wonder why your image field doesn't say string($uri). That is how mine always is (and the OP's).
Also, I'm using drf-yasg == 1.13.0 instead of django-rest-swagger due to django-rest-swagger having low maintenance as of late. Perhaps others in the thread could comment on which they are using.
I think drf-yasg==1.13.0 is causing the problem here. I've just tried it with Base64ImageField and then I've used the built-in serializers.ImageField; this is what I've gotten in both cases:
That is indeed the case. The field you linked breaks the contract of FileField by making it accept a string instead of an UploadedFile.
You can work around this with a custom FieldInspector that returns an appropriate Schema object of type string, which should be placed before the FileFieldInspector.
I don't quite understand what needs to be done, but if this issue were left open it might be useful. We may be able to at least document how to make this work with drf-yasg, which I think would be valuable given it's the best maintained OpenAPI generator available for Django currently.
Do you happen to understand the comment in the link above, and how we could fix it?
@alicertel No problem! I got it from the code at FieldInspector.add_manual_fields from yasg. It seems to work pretty well and it's less work than creating a new FieldInspector..
Activity
johnthagen commentedon Mar 4, 2019
I noticed this too in generated Angular TypeScript. The image variables were always marked with
?, meaning they were optional.shafiyal commentedon Mar 11, 2019
I am also facing the same issue even though i specified required=True in serializer class
baranugur commentedon Mar 15, 2019
I'm using the same serializer setup and mine seems to be working fine. Maybe this is an issue of differences in versions. Here are the versions of Django, Django Extra Fields, Django REST Framework and Django REST Swagger that I have used, so you can compare:
Django==2.1.7,django-extra-fields==1.0.0,djangorestframework==3.9.2,django-rest-swagger==2.2.0.johnthagen commentedon Mar 15, 2019
I wonder if it's because of the
readOnlyproperty? Would areadOnlyproperty make it optional because it's not required when writing to the API? Is this because of how the file is normally returned as a URL rather than the data directly? (I'm just spit balling at this point).baranugur commentedon Mar 15, 2019
ReadOnly fields cannot be required at the same time, therefore it's not possible to have a field where both
requiredandread_onlyare set toTruein the latest version of Django REST Framework.johnthagen commentedon Mar 15, 2019
What's weird is in my case, I never set
read_onlytoTrue. The serializer is aWritableNestedSerializer, but I don't see how that would affect the field.baranugur commentedon Mar 18, 2019
I've used the serializer below:
It still seems to be working fine.
johnthagen commentedon Mar 18, 2019
@baranugur Could you also post your
ImageModel?I wonder why your
imagefield doesn't saystring($uri). That is how mine always is (and the OP's).Also, I'm using
drf-yasg == 1.13.0instead ofdjango-rest-swaggerdue todjango-rest-swaggerhaving low maintenance as of late. Perhaps others in the thread could comment on which they are using.baranugur commentedon Mar 19, 2019
The model that I've created is identical to OP's.
I think
drf-yasg==1.13.0is causing the problem here. I've just tried it withBase64ImageFieldand then I've used the built-inserializers.ImageField; this is what I've gotten in both cases:johnthagen commentedon Mar 19, 2019
Cross posted to axnsan12/drf-yasg#332
johnthagen commentedon Mar 19, 2019
Confirmed this is
drf-yasgspecific (intended) behavior:axnsan12/drf-yasg#332 (comment)
yigitguler commentedon Apr 18, 2019
Marking as completed.
johnthagen commentedon Apr 18, 2019
@yigitguler There may be a way to work around this (at least in documentation): axnsan12/drf-yasg#332 (comment)
I don't quite understand what needs to be done, but if this issue were left open it might be useful. We may be able to at least document how to make this work with
drf-yasg, which I think would be valuable given it's the best maintained OpenAPI generator available for Django currently.Do you happen to understand the comment in the link above, and how we could fix it?
9 remaining items
ilmesi commentedon May 11, 2020
@alicertel No problem! I got it from the code at
FieldInspector.add_manual_fieldsfrom yasg. It seems to work pretty well and it's less work than creating a new FieldInspector..johnthagen commentedon May 26, 2020
I can confirm that @ilmesi's answer works. It seems like the most ideal work-around thus far.
It would be good if this was documented somewhere officially.
WasinTh commentedon May 27, 2020
Agreed. The solution to override Base64FileField is much more simple.
alicertel commentedon Aug 6, 2020
@WasinTh Would you like to update your PR #100 with the suggested solution #66 (comment) ?
Update answer to the easier version as suggested in Hipo#66 (comment)
Update answer to the easier version as suggested in Hipo/drf-extra-fi…
WasinTh commentedon Aug 14, 2020
@alicertel Sure. I've pushed the updated answer on those PRs.
alicertel commentedon Aug 14, 2020
Thanks @WasinTh
Bug/fix travis errors (#28)
Release 1.18 (#29)
Hassanzadeh-sd commentedon Nov 25, 2020
you should change your parser like this :
after that you can check your swagger :
I hope this can help you
Krzyzaku21 commentedon Jan 13, 2022
If you want create no class views but use drf:
using manual_parameters you can override form in swagger and add new functions like default etc to image.
Advance master to release 1.21.7 (#815)
Merge release branches (#897)