我在 django-rest-framework 中将自定义 URL 添加到 ModelViewSet 时遇到问题。这是我的主要 urls.py 的示例
router = routers.DefaultRouter()
router.register(r'post', PostViewSet)
urlpatterns = patterns('',
url(r'^api/', include(router.urls)),
)
我的模型视图集看起来像
class PostViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)
search_fields = ('created')
def pre_save(self, obj):
obj.user = self.request.user
#
# based on the post type this will decide which serializer to use for the data
def get_serializer_class(self):
#
# default is the Text role serializer
return PostSerializer
这对于像这样的网址非常有用
/api/post/
我正在寻找一个固定的日子
/api/post/yyyy/mm/dd/
或者我应该只使用类似的东西
/api/post/?year=&month=&day=