django-rest-framework and custom routes

自作多情 提交于 2020-01-03 03:44:09

问题


I have the following setup.

router = routers.DefaultRouter()
router.register(r'post', PostViewSet)

Then in my urlpatterns

url(r'^api/', include(router.urls)),

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/

That would pull out all the data for that current day.. I'm a bit unsure how to do custom routes in django-rest


回答1:


The part of the Django Rest Framework docs you're looking for is that on filtering against the URL.

The basic idea is that you override get_queryset to return a filtered QuerySet matching parameters you define in your URL conf.

The Django ORM field lookups you'll need are year, month and day, which start here in the QuerySet API reference.

I hope that helps.



来源:https://stackoverflow.com/questions/18156291/django-rest-framework-and-custom-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!