When rewriting the ProductDetail class using a generic view,
I don’t understand how the queryset attribute is set to Product.objects.all().
It seems to me that for ProductDetail, the queryset should return a single product according to its id.
Shouldn’t it be Product.objects.get(pk=pk)?
What am I missing?
Hi,
A viewset provides a set of actions. They combine the most common operations made on a model. The queryset you are providing when setting it up is for the list action (which is a get request for getting a collection of resources). You can overwrite the action-methods on the viewset and set another queryset if you wish for the retrieve-method (but it is not necessary in this case). You can use a router in combination with the viewset (in your url-module) and then Django will create urls for all actions on the viewset. Including a one with collection/id.
You can read more about viewset a here: