django rest 框架中产品的详细视图集
Detail viewset for product in django rest framework
我只需要根据给定的 slug 在我的 VeiwSet 中获取一个产品对象,我查看了文档,但找不到解决此问题的任何方法。我还需要从 url 路径获取 slug,但我也不知道该怎么做。显然下面的代码不起作用。
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))
]
ViewSets
允许您将 ListView
和 DetailView
合并到一个视图中。
所以你不需要两个不同的视图来处理这两个动作。
现在,如果你想在 url 中使用 slug 而不是默认情况下的 id
,你只需在序列化程序和视图中指定 lookup_field
,如下所示:
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)),
]
现在您可以查询 http://localhost:8000/
的 产品列表 和 http://localhost:8000/product_slug
的 产品详细信息 和 product_slug
作为鼻涕虫。
更多关于 Django ViewSets and Routers
我只需要根据给定的 slug 在我的 VeiwSet 中获取一个产品对象,我查看了文档,但找不到解决此问题的任何方法。我还需要从 url 路径获取 slug,但我也不知道该怎么做。显然下面的代码不起作用。
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))
]
ViewSets
允许您将 ListView
和 DetailView
合并到一个视图中。
所以你不需要两个不同的视图来处理这两个动作。
现在,如果你想在 url 中使用 slug 而不是默认情况下的 id
,你只需在序列化程序和视图中指定 lookup_field
,如下所示:
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)),
]
现在您可以查询 http://localhost:8000/
的 产品列表 和 http://localhost:8000/product_slug
的 产品详细信息 和 product_slug
作为鼻涕虫。
更多关于 Django ViewSets and Routers