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

Add serializer method field with params #9132

Closed
wants to merge 1 commit into from
Closed

Add serializer method field with params #9132

wants to merge 1 commit into from

Conversation

SergeiVElfimov
Copy link

Description

Hello dear maintainers, it's my first contribution to django rest framework so I hope I haven't done anything wrong.

I often encounter the need of reusing an existing method for a SerializerMethodField field with additional parameters.

Therefore, I decided to inherit from the SerializerMethodField class and pass the arguments included in the class to the method for greater flexibility.

For example:

class ExampleSerializer(Serializer):
        extra_info_bar_1 = SerializerMethodWithParamsField(method_name='foo', bar='bar_1', ...)
        extra_info_bar_2 = SerializerMethodWithParamsField(method_name='foo', bar='bar_2', ...)

        def foo(self, obj, **kwargs):
            bar = kwargs.get('bar', None)
            return str(obj) + bar

Thanks for reading and please let me know if there are any changes that could be made.

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need this in the DRF core?

@SergeiVElfimov
Copy link
Author

@auvipy Why not? It seems to me a very useful feature.

@kevin-brown
Copy link
Member

I believe the current stance is that if it can be a third party package, it's better to have it as one. The DRF core changes slowly and hasn't really changed much in the past years, so you're better off having stuff like this in one of the faster moving external packages.

@SergeiVElfimov
Copy link
Author

@kevin-brown Thanks for the detailed answer. I am very sorry that the changes to the DRF core slowly, because I think one of the best projects. I'm closing pull request.

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

Successfully merging this pull request may close these issues.

3 participants