django-rest custom url in a ModelViewSet -
i'm having issue adding custom url modelviewset in django-rest-framework. here's example of main urls.py
router = routers.defaultrouter() router.register(r'post', postviewset) urlpatterns = patterns('', url(r'^api/', include(router.urls)), )
my modelviewset looks like
class postviewset(viewsets.modelviewset): """ api endpoint allows users 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 post type decide serializer use data def get_serializer_class(self): # # default text role serializer return postserializer
that works great url like
/api/post/
i'm looking set day like
/api/post/yyyy/mm/dd/
or should use
/api/post/?year=&month=&day=
pull list method out separate view:
post_list = postviewset.as_view({'get': 'list'})
then map date-based lookup url would, setting parameters year, month , day.
in get_queryset
can check if these kwargs set and, if so, filter queryset.
Comments
Post a Comment