I have a pretty simple urls.py:

router = routers.DefaultRouter()
router.register(r'products', views.ProductViewSet, basename = "product")

urlpatterns = [
    url('^api/', include(router.urls)), ...]

where Product looks like this:

class Product(models.Model):
    user = models.ManyToManyField(User, related_name="User", blank = True)
    name = models.CharField('name', max_length=120)

What I want is a view that returns products from a user, if a user is handed to it, and returns all products when no user is submitted. I read multiple posts 1, 2, 3 but I could not solve it. Getting all products works:

class ProductViewSet(LoginRequiredMixin, viewsets.ViewSet):
    login_url = "/login/"

    def list(self, request):
        data = [{"products": Product.objects.all(),}]
        results = ProductSerializer(data, many = True).data
        return Response(results)

Serializer.py:

class ProductSerializer(serializers.Serializer):
    products = serializers.CharField(max_length = 120)

Can I add another method to my ProductViewSet, and how do I have to alter the urls.py so that I can call (for User with id=1):

http://127.0.0.1:8002/api/products/1