We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
I'd like to model the following type:
class ItemSerializer(ModelSerializer): children = ItemSerializer(many=True)
Due to Python limitations with how classes are defined this is not possible.
I thought about looking at https://github.com/heywbj/django-rest-framework-recursive, but this hasn't been updated for 5 years and there are reports about it not working with modern Django versions:
Finally, I thought of trying a SerializerMethod like:
SerializerMethod
class ItemSerializer(ModelSerializer): children = SerializerMethodField() def get_children(self, instance: Item) -> list[Item]: children = instance... return ItemSerializer(children, many=True).data
But type hints containing Model subclasses don't seem to be supported, which makes sense.
Model
I was curious if there was some other way to get a nested serializer to be registered/recognized with drf-spectacular.
drf-spectacular
The text was updated successfully, but these errors were encountered:
Interesting, using the Serializer as a type hint seems to work:
Serializer
from __future__ import annotations class ItemSerializer(ModelSerializer): children = SerializerMethodField() def get_children(self, instance: Item) -> ItemSerializer: children = instance... return ItemSerializer(children, many=True).data
Looks like this is documented here:
It takes either basic types or a Serializer as argument. In case of basic types (e.g. str, int, etc.) a type hint is already sufficient.
It might be more discoverable if the opening page that lists SerializerMethodField (https://drf-spectacular.readthedocs.io/en/latest/readme.html) linked to this subsection.
SerializerMethodField
Sorry, something went wrong.
Successfully merging a pull request may close this issue.
I'd like to model the following type:
Due to Python limitations with how classes are defined this is not possible.
I thought about looking at https://github.com/heywbj/django-rest-framework-recursive, but this hasn't been updated for 5 years and there are reports about it not working with modern Django versions:
Finally, I thought of trying a
SerializerMethod
like:But type hints containing
Model
subclasses don't seem to be supported, which makes sense.I was curious if there was some other way to get a nested serializer to be registered/recognized with
drf-spectacular
.The text was updated successfully, but these errors were encountered: