In my django project > myapp > api > views.py (Django REST framework (DRF)) is a class MyModelViewset(viewsets.ModelViewSet). This Viewset shall provide a retrieve (model based), but my data are not unique by a single entry (model field). And a pk is not helping, because it can not be observed.

My data are unique by 3 fields, represented in the model as field a, b, c.

So for a retrieve I am the opinion that I need 3 parameters: .../api/mymodel?a=1&b=2&c=3

For UI I use openapi swagger with the python package drf-yasg

Right now, it looks because of the set lookup field: lookup_field = "a" like this: .../api/mymodel/1?b=2&c=3

The problem is, that I can not customize field 'a' in the swagger-API and I can not access the retrieve by parameter a.

Perhaps I am totally wrong in the Django like / Swagger like workflow, I am open for proposals

Please be gracious, this is my first question ever

To achieve some customization I tried to add some openapi.parameter() fields:

I added the 2 parameters b and c to the openapi.Parameter() by using django drf-yasg (I need a solution for drf-yasg because I did really much with that)

from django.utils.decorators import method_decorator
from rest_framework import viewsets
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

class MyModelViewset(viewsets.ModelViewSet):
    ...
    b = openapi.Parameter('b', openapi.IN_QUERY,
                                description="b",
                                type=openapi.TYPE_NUMBER,
                                required=True,
                                default=2
                                )
    c = openapi.Parameter('c', openapi.IN_QUERY,
                                description="c",
                                type=openapi.TYPE_NUMBER,
                                required=True,
                                default=3
                                )
    @method_decorator(
        decorator=swagger_auto_schema(
            manual_parameters=[b, c],
            responses={200: response_schema} # unimportent right now
            ))
    def retrieve(self, *args, **kwargs):
        return super().retrieve(*args, **kwargs)
0

There are 0 best solutions below