如何适应APIView& urls.py中的ViewSet视图

时间:2016-10-14 17:58:16

标签: django django-views django-rest-framework django-urls

如何编写urls.py文件以容纳从APIView和ViewSet创建的视图。

entity.views.py

from .models import Entity
from .serializers import EntitySerializer

class EntityViewSet(DefaultsMixin, ListCreateRetrieveUpdateViewSet):
    """
    """
    queryset = Entity.objects.all()
    serializer_class = EntitySerializer
    filter_fields = ('id', 'entity_number')

class PersonProfileList(APIView):
    """
    person profile
    """
    def get(self, request, format=None):
        pass

entity.urls.py

from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from rest_framework.urlpatterns import format_suffix_patterns

from . import views

entity_router = DefaultRouter()
entity_router.register(r'entity', views.EntityViewSet)

urlpatterns = [
    url(r'profile/$', views.PersonProfileList.as_view(), name='profile_list'),  # Is this correct?
    url(r'profile/(?P<pk>[0-9]+)/$', views.PersonProfileList.as_view(), name='profile_detail'),
]

urlpatterns = format_suffix_patterns(urlpatterns)

主要urls.py

from django.conf.urls import include, url
from django.contrib import admin

from entities.urls import entity_router, urlpatterns

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^entities/', include(entity_router.urls)),  #This I know works
    url(r'^entities/', include(urlpatterns.url)),  # This throws errors
]

在同一个URL文件中容纳这两种类型的视图并将它们显示在一个/entity下的最佳方法是什么,这与现在获取两个/实体条目时不同。此外,一旦我进入可浏览API的/ entity页面,如何使/entity/profile可查看,因为它现在只显示/entity。请参阅图像以获取指南。

根页 Root URL Page

实体页面 Entities URL Page

0 个答案:

没有答案