我对以下情况有些困惑。我想使用Django Rest Framework为购物车应用程序构建REST API,但是由于遗留要求,我需要使用嵌套URL。
通常,我有两个资源AppUsers和Carts。这两个资源都在默认的/ appUsers /和/ carts /端点上可用。然后,我尝试使用嵌套路由器获取特定用户的购物车详细信息视图,以便将其定位为/ appUsers / app_user_pk / cart /而不是/ carts / pk /,因为每个AppUser无论如何只能拥有一个购物车。
这是我的设置:
models.py
class AppUser(models.Model):
_id = models.AutoField(primary_key=True)
class Meta:
default_related_name = 'app_users'
class Cart(models.Model):
app_user = models.OneToOneField(
'AppUser',
on_delete=models.CASCADE,
related_query_name='cart',
)
class Meta:
default_related_name = 'carts'
def __str__(self):
return "{user} cart".format(user=self.app_user._id)
serializers.py
class AppUserSerializer(serializers.HyperlinkedModelSerializer):
cart = serializers.HyperlinkedIdentityField(view_name='cart-detail')
class Meta:
model = AppUser
fields = '__all__'
class CartSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Cart
fields = '__all__'
views.py
class AppUserViewSet(viewsets.ModelViewSet):
"""
Model viewset for AppUser model
"""
queryset = AppUser.objects.all()
serializer_class = AppUserSerializer
class CartViewSet(viewsets.ModelViewSet):
"""
List all carts, or create new / edit existing product.
"""
queryset = Cart.objects.all()
serializer_class = CartSerializer
def get_queryset(self):
if 'app_user_pk' in self.kwargs:
return Cart.objects.filter(app_user=self.kwargs['app_user_pk'])
return Cart.objects.all()
urls.py
router = DefaultRouter()
router.register(r'appUsers', views.AppUserViewSet)
router.register(r'carts', views.CartViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^appUsers/(?P<app_user_pk>[0-9]+)/cart/$', views.CartViewSet.as_view({'get': 'retrieve'}), name='cart-detail'),
]
但是,每当我尝试新的东西时,我都无法正常工作,但遇到了另一个问题。所以我想知道完成这项任务的最佳方法是什么?
本质上,我只想拥有一个购物车的购物车详细信息视图,一个AppUser可以在/ appUsers / app_user_pk / cart /
解决方案:
我使用了可接受的答案来解决上述问题。另外,我在/ appUsers / app_user_pk / cart / products中有另一个ModelViewSet,然后我使用了drf-nested-routers的NestedDefaultRouter在如下cart /产品上注册了NestedDefaultRouter:
cart_products_router = routers.NestedDefaultRouter(router, r'appUsers', lookup='app_user')
cart_products_router.register(r'cart/products', views.CartProductViewSet, base_name='cartproduct')
答案 0 :(得分:1)
您可以create a custom method至AppUserViewSet
:
class AppUserViewSet(viewsets.ModelViewSet):
"""
Model viewset for AppUser model
"""
queryset = AppUser.objects.all()
serializer_class = AppUserSerializer
@action(detail=True)
def cart(self, request, pk):
obj = Cart.objects.get(app_user_id=pk)
serializer = CartSerializer(obj)
return Response(serializer.data)