Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to declare ImageField as required #332

Closed
johnthagen opened this issue Mar 19, 2019 · 5 comments
Closed

Unable to declare ImageField as required #332

johnthagen opened this issue Mar 19, 2019 · 5 comments

Comments

@johnthagen
Copy link
Collaborator

johnthagen commented Mar 19, 2019

Cross posted from Hipo/drf-extra-fields#66

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:
image

How can I make the ImageField as required in swagger POST request?

In the other thread we seem to have narrowed the problem down to drf-yasg, testing with drf-yasg==1.13.0.

@axnsan12
Copy link
Owner

A request body can only contain files when it has the multipart content type. You need to set the view parser_classes to (MultipartParser,) (no json).

@johnthagen
Copy link
Collaborator Author

@axnsan12 Base64ImageField returns the contents of the field as base64 encoded ascii inline. It's used to make the API json-only so that multipart doesn't need to handled. This simplifies (in one degree) the overall API.

It sounds like drf-yasg assumes that all files must be sent multipart instead of inline?

This SO post explains the problem and Base64ImageField is one possible solution: https://stackoverflow.com/a/4083908/1398841

@axnsan12
Copy link
Owner

axnsan12 commented Mar 19, 2019

It sounds like drf-yasg assumes that all files must be sent multipart instead of inline?

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.

@johnthagen
Copy link
Collaborator Author

@axnsan12 I'd like to try to fix this for drf-extra-fields. I'm not very familiar with custom FieldInspector's. At a high level, what would need to be placed where? Would this be added to Base64ImageField? Are there any links to docs or guides that will help me learn what all the steps I need to take are and how to test?

Thanks!

@johnthagen
Copy link
Collaborator Author

PR with doc fix for this issue: #445

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants