-
Notifications
You must be signed in to change notification settings - Fork 61
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 expandable SerializerMethodFields
#54
Comments
I like the idea of supporting DRF's class UserSerializer(serializers.ModelSerializer):
friends = HyperlinkFIeld()
class Meta:
model = User
expandable_fields = {'friends': serializer.SerializerMethodField}
def get_friends(self, obj, expand, fields, omit):
# serialize friends using custom logic We could add a custom |
I think this breaks the model of expanding a field. Getting back the non expanded version of |
One workaround that I've used is to convert your method field to an annotation on the queryset and expand on that. It also has the benefit of being much faster in most cases. |
Hello, I have a few large
SerializerMethodFields
on my serializer, which return iterables that are not a FK to my model. Something like this:As far as I know I cannot directly leverage
drf-flex-fields
to make this field expandable, becauseexpandable_fields
are a statically defined dict that takes a serializer class or tuple.I have hacked around this by making these fields omitted by default, unless declared in the
expand
argument:It works well enough for my purposes, but I thought I'd pitch this as a feature request, in case it is helpful or others have solved the problem differently. Cheers, and thanks for the library!
The text was updated successfully, but these errors were encountered: