yezyilomo / django-restql

Turn your API made with Django REST Framework(DRF) into a GraphQL like API.
https://yezyilomo.github.io/django-restql
MIT License
620 stars 43 forks source link

use django-restql in api_view #205

Closed 3lf closed 3 years ago

3lf commented 3 years ago

how can I use django-restql in api_view? for example:

@api_view()
def getInputCartWithCartJidView(request, cart_jid):
    InputCarts = InputCart.objects.get(cart_jid=cart_jid)
    return Response({"status": "success",
                     "data": InputCartSerializer(obj).data},
                    status=status.HTTP_200_OK)
3lf commented 3 years ago

I found it after hours read code :| just need add ,context={'request': request,} to serializer

for example: change InputCartSerializer(obj).data to InputCartSerializer(obj,context={'request': request,}).data

yezyilomo commented 3 years ago

I found it after hours read code :| just need add ,context={'request': request,} to serializer

for example: change InputCartSerializer(obj).data to InputCartSerializer(obj,context={'request': request,}).data

Yes DynamicFieldsMixin needs request to work cuz that's where it gets the query, so passing request to the context is all you need in your serializer.