Make serializers.py::Serializer::get_fields a classmethod
#9889
Unanswered
Checkroth
asked this question in
Ideas & Suggestions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
General Idea
The
get_fieldsmethod of the Serializer class deepcopies_declared_fields, which is itself configured via a classmethod called by the metaclass's__new__method: https://github.com/encode/django-rest-framework/blob/main/rest_framework/serializers.py#L309-L311This field will always be set, as
__new__will be called when declaring a Serializer class withSerializerMetaClassas ametaclass.Given that
_declared_fieldsshould be accessible before class instantiation, it would be helpful to makeget_fieldsa classmethod.Specific use-case
My project keeps a mapping of
unique_name: SerializerClass.I'm attempting to make use of this mapping to implement extra validation on declared fields, and generate some automated documentation about those serializers and their fields.
In order to do so, we have to do one of a few non-optimal things:
_declared_fieldsmethodgetattron the classdict(SerializerClass).get("_declared_fields")get_fieldsI would like to open a PR that changes
get_fieldsto aclassmethod, in accordance with CONTRIBUTING.md would like to open a discussion about the change before doing so.Beta Was this translation helpful? Give feedback.
All reactions