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"),
]
request.DATAhasn't existed for a while), I'd highly recommend upgrading and switching that torequest.data.