0

i am new to DRF, and implemneting class based views and a sample example is this i have tried

@api_view(['GET', 'POST'])
class ProductList(APIView):
    print "inside"

    def get_user_products(self, request, user_id, format=None):
        products = Product.objects.all(user_id=user_id)
        serializer = ProductSerializer(products, many=True)
        return Response(serializer.data)

    def get_seller_products(self, request, seller_id, format=None):
        products = Product.objects.filter(seller_id=seller_id)
        serializer = ProductSerializer(products, many=True)
        return Response(serializer.data)

    def post(self, request, user_id, seller_id, format=None):
        serializer = ProductSerializer(data=request.DATA, context={'request':request})
        if serializer.is_valid():
            serializer.object.user = User.objects.get(id=user_id)
            serializer.object.seller = Seller.objects.get(id=seller_id)
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

i this class for my product apis , but i am not sure how can i access individual methods to access the corresponding result, like how can i hit get_user_products or get_seller_products

Right now when i call this api like this http://localhost:8000/products

as GET method this class executed and it prints "inside" as you can see, but how do i call the methods inside it

Here is the url.py for this app

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^login$', 'userapp.views.login_user', name="login"),
    url(r'^products$', 'productapp.views.ProductList', name="product-list"),
]
1
  • It looks like you may be using a very old version of DRF (request.DATA hasn't existed for a while), I'd highly recommend upgrading and switching that to request.data. Commented Apr 10, 2016 at 12:54

1 Answer 1

3

You're not supposed to use the api_view decorator to decorate class-based views; you use api_view with function-based views.

If you want to use a class-based view, like the one in your example, remove the print statement, and the api_view decorator, and include the view in your urlconf by importing the view, and using ProductList.as_view(), as shown in the tutorial. In your case, the urlconf might look like this:

from productapp.views import ProductList

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^login$', 'userapp.views.login_user', name="login"),
    url(r'^products$', ProductList.as_view(), name="product-list"),
]

Also, keep in mind, that using strings containing import paths for your views in urlpatterns is deprecated, and support for this will be removed in an upcoming version of Django. Instead, import all your views inside your urlconf, and use the view functions directly, for example:

from productapp.views import ProductList
from userapp.views import login_user

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^login$', login_user, name="login"),
    url(r'^products$', ProductList.as_view(), name="product-list"),
]
Sign up to request clarification or add additional context in comments.

4 Comments

done the changes, but now it is throwing GET is not allowed , and also can you explain how would i access the the various functions inside the class and what will be apis for that ?
Well, GET is not supported by your view, because you didn't implement the get method in that class. As for your other question, I would recommend doing a Python language tutorial.
I am trying to dispatch the request to a method, how do I provide the proper request instead of HttpRequst (WsgiRequest)
Please, ask a new question, Akshay. This is not related to my answer in any way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.