具有多个查找参数的 django-rest-framework 自定义视图集检索
django-rest-framework custom viewset retrive with multiple lookup args
我有两个型号,A 和 B。
模特长得像
class A(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
slug = models.CharField(max_length=100)
class B(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
slug = models.CharField(max_length=100)
A = models.foreignkey(A, models.SET_NULL, blank=true)
序列化程序
Class ASerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = A
fields = '__all__'
lookup_field = 'slug'
extra_kwargs = {'url': {'lookup_field': 'slug'}}
Class BSerializer(serializers.ModelSerializer):
class Meta:
model = B
fields = '__all__'
模型视图集
from rest_framework import viewsets, permissions
from rest_framework.response import Response
from rest_framework.decorators import action
from .models import (A, B)
from .serializers import (ASerializer, BSerializer)
Class AViewSet(viewsets.ModelViewSet):
queryset = A.objects.all()
serializer_class = ASerializer
permission_classes = (permissions.DjangoModelPermissionsOrAnonReadOnly, )
lookup_field = 'slug'
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
bs = B.objects.filter(A=instance.id)
serializer = BSerializer(bs, many=True)
return Response(serializer.data)
我想访问 url 类似
的内容
Url: ^A/<slug>/B/slug>/$
我浏览了 Django Restframework 文档,发现我们可以添加自定义 url,例如(drf-custom-rounting。我不知道如何访问上面的 url 模式。
如何定制这样的?
编辑:
我已经解决了我的问题。我找到了类似类型的问题解决方案 here。感谢您的所有回复。
在你的urls.py
from django.conf.urls import url, include
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'yourregex', views.AViewSet)
urlpatterns = [
url(r'^api/', include(router.urls)),
]
我认为您正在寻找一种制作嵌套 url 的方法。
DRF 文档推荐的方式是使用这个包:https://github.com/alanjds/drf-nested-routers
用法示例:
首先创建视图集:
Class BViewSet(viewsets.ModelViewSet):
queryset = B.objects.all()
serializer_class = BSerializer
lookup_field = 'slug'
def retrieve(self, request, a_slug=None, b_slug=None):
a = self.get_object()
# Now you retrieve all B related to A
bs = B.objects.filter(A=a)
serializer = BSerializer(bs, many=True)
return Response(serializer.data)
然后使用 drf-nested-routers 注册您的视图集:
from rest_framework_nested import routers
from .viewsets import AViewSet, BViewSet
a_router = routers.SimpleRouter()
a_router.register(r'A', AViewSet)
b_router = routers.NestedSimpleRouter(a_router, r'B')
b_router.register(r'B', BViewSet)
urlpatterns = patterns('',
url(r'^', include(a_router.urls)),
url(r'^', include(b_router.urls)),
)
我有两个型号,A 和 B。
模特长得像
class A(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
slug = models.CharField(max_length=100)
class B(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
slug = models.CharField(max_length=100)
A = models.foreignkey(A, models.SET_NULL, blank=true)
序列化程序
Class ASerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = A
fields = '__all__'
lookup_field = 'slug'
extra_kwargs = {'url': {'lookup_field': 'slug'}}
Class BSerializer(serializers.ModelSerializer):
class Meta:
model = B
fields = '__all__'
模型视图集
from rest_framework import viewsets, permissions
from rest_framework.response import Response
from rest_framework.decorators import action
from .models import (A, B)
from .serializers import (ASerializer, BSerializer)
Class AViewSet(viewsets.ModelViewSet):
queryset = A.objects.all()
serializer_class = ASerializer
permission_classes = (permissions.DjangoModelPermissionsOrAnonReadOnly, )
lookup_field = 'slug'
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
bs = B.objects.filter(A=instance.id)
serializer = BSerializer(bs, many=True)
return Response(serializer.data)
我想访问 url 类似
的内容Url: ^A/<slug>/B/slug>/$
我浏览了 Django Restframework 文档,发现我们可以添加自定义 url,例如(drf-custom-rounting。我不知道如何访问上面的 url 模式。
如何定制这样的?
编辑: 我已经解决了我的问题。我找到了类似类型的问题解决方案 here。感谢您的所有回复。
在你的urls.py
from django.conf.urls import url, include
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'yourregex', views.AViewSet)
urlpatterns = [
url(r'^api/', include(router.urls)),
]
我认为您正在寻找一种制作嵌套 url 的方法。
DRF 文档推荐的方式是使用这个包:https://github.com/alanjds/drf-nested-routers
用法示例:
首先创建视图集:
Class BViewSet(viewsets.ModelViewSet):
queryset = B.objects.all()
serializer_class = BSerializer
lookup_field = 'slug'
def retrieve(self, request, a_slug=None, b_slug=None):
a = self.get_object()
# Now you retrieve all B related to A
bs = B.objects.filter(A=a)
serializer = BSerializer(bs, many=True)
return Response(serializer.data)
然后使用 drf-nested-routers 注册您的视图集:
from rest_framework_nested import routers
from .viewsets import AViewSet, BViewSet
a_router = routers.SimpleRouter()
a_router.register(r'A', AViewSet)
b_router = routers.NestedSimpleRouter(a_router, r'B')
b_router.register(r'B', BViewSet)
urlpatterns = patterns('',
url(r'^', include(a_router.urls)),
url(r'^', include(b_router.urls)),
)