django-rest框架和自定义路由 [英] django-rest-framework and custom routes
问题描述
我有以下设置。
router = routers.DefaultRouter()
router.register(r'post', PostViewSet)
然后在我的urlpatterns
Then in my urlpatterns
url(r'^api/', include(router.urls)),
然后在我的views.py
Then in my views.py
class PostViewSet(viewsets.ModelViewSet):
现在,对于我的用例,我完全可以这样做,但是我也想做这样的事情来抓住某一天的数据
Now that works perfectly for my use case but I also want to do something like this to grab data from a certain day
/api/post/2013/08/09/
这将拉出当前的所有数据..我有点不确定如何在django-rest中定制路由
That would pull out all the data for that current day.. I'm a bit unsure how to do custom routes in django-rest
推荐答案
您正在寻找的Django Rest Framework文档的一部分是在过滤e网址。
The part of the Django Rest Framework docs you're looking for is that on filtering against the URL.
基本思想是覆盖 get_queryset
以返回您在URL conf中定义的过滤的QuerySet匹配参数。
The basic idea is that you override get_queryset
to return a filtered QuerySet matching parameters you define in your URL conf.
您需要的Django ORM字段查找是 year
, month
和 day
,这从这里开始在QuerySet API参考中。
The Django ORM field lookups you'll need are year
, month
and day
, which start here in the QuerySet API reference.
我希望有所帮助。
这篇关于django-rest框架和自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!