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

Support for SerializerMethodField? #137

Closed
therefromhere opened this issue Jun 2, 2018 · 3 comments
Closed

Support for SerializerMethodField? #137

therefromhere opened this issue Jun 2, 2018 · 3 comments
Assignees
Milestone

Comments

@therefromhere
Copy link
Contributor

It would be useful for drf-yasg to support SerializerMethodField - I've not tried this yet, but I wonder if using py3.5+ type hinting to get the return type of the serializer method would work?

@therefromhere
Copy link
Contributor Author

Actually thinking about this, hinting the return type wouldn't help much, since that's not going to give you the serializer class.

My main use case for this is where I have a SerializerMethodField that uses a serializer class in its implementation. So maybe it'd be more useful to have a decorator for use with the SerializerMethodField methods a bit like swagger_auto_schema works on actions?

Something like:

from django.contrib.auth.models import User
from django.utils.timezone import now
from rest_framework import serializers

class SomeOtherSerializer(serializers.Serializer):
   ...

class UserSerializer(serializers.ModelSerializer):
    other_stuff = serializers.SerializerMethodField()

    class Meta:
        model = User

    @swagger_serializer_method(serializer_class=SomeOtherSerializer)
    def get_other_stuff(self, obj):
        # ...calculate something...
        return SomeOtherSerializer(something).data

@axnsan12
Copy link
Owner

axnsan12 commented Jun 2, 2018

So maybe it'd be more useful to have a decorator for use with the SerializerMethodField methods a bit like swagger_auto_schema works on actions?`

This could work and seems like a clean enough API.

@swagger_serializer_method(serializer_class=SomeOtherSerializer)

Something to consider is that the return value will not necessarily be a serializer-generated object (i.e. it could be a plain int, or a list, etc)

@KingMob
Copy link

KingMob commented Aug 2, 2018

Hmm, is there a work-around for the time being? We have a similar issue where we're returning an int, but YASG thinks it's a string.

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

3 participants