Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Detail viewset for product in django rest framework

I need to get just one product object in my VeiwSet based on a given slug, I looked into the docs, but I can't find any solution to this problem. I need to get the slug from the url path aswell, but I don't know how to do it too. Obviously the code below doesn't work.

product/serializers.py

from rest_framework import serializers
from .models import Product

class ProductSerializer(serializers.ModelSerializer):
    image = serializers.ImageField(required=True)
    class Meta:
        model = Product
        fields = ("name", "description", "slug", "id", "price", "stock", "image", "category")

product/views.py

from django.http.response import JsonResponse
from rest_framework import serializers, viewsets
from rest_framework.response import Response
from django.http import JsonResponse
from .serializers import ProductSerializer
from .models import Product

class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all().order_by('name')
    serializer_class = ProductSerializer

class ProductDetailViewSet(viewsets.ViewSet, slug):
    queryset = Product.objects.filter(slug=slug)
    serializer_class = ProductSerializer

product/urls.py

from rest_framework import routers, urlpatterns
from django.urls import path, include
from .views import ProductViewSet, ProductDetailiewSet

router = routers.DefaultRouter()
router.register(r'', ProductViewSet)


urlpatterns = [
    path('<str:slug>/',),
    path('', include(router.urls))
]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

ViewSets allows you to combine ListView and DetailView into one view. So you don't need two different views to handle the both actions.

Now if you want to use slug in the url instead of the id by default, you just have to specify lookup_field in the serializer and the view like this :

serializers.py

class ProductSerializer(serializers.ModelSerializer):
    image = serializers.ImageField(required=True)
    class Meta:
        model = Product
        fields = ("name", "description", "slug", "id", "price", "stock", "image", "category")
        lookup_field = 'slug'
        extra_kwargs = {
            'url': {'lookup_field': 'slug'}
        }

views.py

class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all().order_by('name')
    serializer_class = ProductSerializer
    lookup_field = 'slug'

urls.py

router = routers.DefaultRouter()
router.register(r'', ProductViewSet)

urlpatterns = [
    url(r'', include(router.urls)),
]

Now you can query http://localhost:8000/ for products list and http://localhost:8000/product_slug for product detail with product_slug as slug. More about Django ViewSets and Routers

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!