Drf-yasg: How to document path parameter defined in router url?

Created on 27 Jun 2019  路  3Comments  路  Source: axnsan12/drf-yasg

Is it possible to document a path parameter that is defined in an url, e.g. document 'currency' in url(r'^v1/object/(?P<currency>\busd\b|\beur\b|\bkrw\b)/$', View.as_view())? Is yes, how? Thanks!

Most helpful comment

Hi @csdenboer,
This is the way I did it on a project of mine:

urls.py

urlpatterns = [
    ...,
    url(
        r'website/(?P<website_pk>[^/.]+)/product/(?P<product_id>[^/.]+)/reco/',
        get_similar_products_by_client_reference
    ),
]

search.py

@swagger_auto_schema(tags=["Recommendation"],
                     method="GET",
                     responses={"200": LightProductSerializer(many=True)},
                     manual_parameters=[
                         Parameter(name="product_id",
                                   required=True,
                                   type="string",
                                   in_="path",
                                   description="Client reference for the product",)
                     ])
@api_view(["GET"])
@permission_classes((ReadRecommendationPermission,))
def get_similar_products_by_client_reference(request, website_pk, product_id):
    ...

And this is what you get:
image

All 3 comments

Hi @csdenboer,
This is the way I did it on a project of mine:

urls.py

urlpatterns = [
    ...,
    url(
        r'website/(?P<website_pk>[^/.]+)/product/(?P<product_id>[^/.]+)/reco/',
        get_similar_products_by_client_reference
    ),
]

search.py

@swagger_auto_schema(tags=["Recommendation"],
                     method="GET",
                     responses={"200": LightProductSerializer(many=True)},
                     manual_parameters=[
                         Parameter(name="product_id",
                                   required=True,
                                   type="string",
                                   in_="path",
                                   description="Client reference for the product",)
                     ])
@api_view(["GET"])
@permission_classes((ReadRecommendationPermission,))
def get_similar_products_by_client_reference(request, website_pk, product_id):
    ...

And this is what you get:
image

it works like a charm!

Is there any possibility to set the response with multiple serializers lists?
For example when my view return the next response:

Response({
'listA': ASerializer(many=True),
'listB':BSerializer(many=True),
'listC': CSerializer(many=True)
})

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maikotz picture maikotz  路  4Comments

nicholasgcoles picture nicholasgcoles  路  4Comments

geekashu picture geekashu  路  5Comments

phihag picture phihag  路  5Comments

jaumard picture jaumard  路  5Comments