Skip to content

Examples for NestedHyperlinkedRelatedField (from drf-nested-routers) #1498

Description

@TauPan

This is sort of a proposal / feature request.

I wanted to see proper examples for those urls instead of https://example.com so I came up with this:

class NestedHyperlinkedRelatedFieldExtension(OpenApiSerializerFieldExtension):
    target_class = 'rest_framework_nested.relations.NestedHyperlinkedRelatedField'
    priority = 1

    def map_serializer_field(
            self,
            auto_schema: 'AutoSchema',
            direction: Direction) -> _SchemaType:
        ret = auto_schema._map_serializer_field(
            self.target, direction, bypass_extensions=True)
        ret['examples'] = [
            'https://example.com' +
            self.target.reverse(
                self.target.view_name,
                [1] * (len(self.target.parent_lookup_kwargs) + 1))]
        return ret

Is there a way to have the url schema and hostname not be static? (I don't mind really, so this is good enough for me.)

Would this maybe be considered for inclusion in a future version of drf-spectacular?

Best regards
Friedel

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions