drf-extra-fields: Unable to make the ImageField as required in Swagger POST API
My model:
class Image(models.Model):
image=models.ImageField(upload_to='photos')
name=models.CharField(max_length=40,unique=False)
My serializer:
class imagesSerializer(serializers.ModelSerializer):
image = Base64ImageField(required=True)
class Meta:
model = Image
fields = ('id','name','image')
My swagger view:

How to make the imagefield as required in swagger post request ?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 5
- Comments: 29 (19 by maintainers)
Commits related to this issue
- Update answer to the easier version as suggested in https://github.com/Hipo/drf-extra-fields/issues/66#issuecomment-626755554 — committed to WasinTh/drf-extra-fields by WasinTh 4 years ago
- Update answer to the easier version as suggested in https://github.com/Hipo/drf-extra-fields/issues/66#issuecomment-626755554 — committed to WasinTh/drf-yasg by WasinTh 4 years ago
- Bug/fix travis errors (#28) * Add sample code to handle Base64 fields in drf-extra-fields project * Fix format for .rst * Update Redoc to 2.0.0-rc36 * Update answer to the easier version as ... — committed to JoelLefkowitz/drf-yasg by JoelLefkowitz 4 years ago
- Release 1.18 (#29) * Add sample code to handle Base64 fields in drf-extra-fields project * Fix format for .rst * Update Redoc to 2.0.0-rc36 * Update answer to the easier version as suggested... — committed to JoelLefkowitz/drf-yasg by JoelLefkowitz 4 years ago
- Advance master to release 1.21.7 (#815) * Add sample code to handle Base64 fields in drf-extra-fields project * Fix format for .rst * Update answer to the easier version as suggested in https:/... — committed to axnsan12/drf-yasg by deleted user a year ago
In Swagger API, I’m using the following setup to show the field as required without any problems:
And to use “string” format instead of an URL, you will need to create the field with use_url=False.
you should change your parser like this :
after that you can check your swagger :
I hope this can help you
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.
@ilmesi Thank you for the suggestion. This seems pretty simple to me compered to creating a FieldInspector. What do you think @WasinTh ?
@alicertel Sure. I’ve pushed the updated answer on those PRs.
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: