我正在使用基于函数的API
{host}:{port}/api/food/<year>/<month>/<day>/
现在,我对模型食品有了新的看法。
{host}:{port}/food/
我希望将这个apis集成到一个ModelViewSet中,所以我在下面做了。
{host}:{port}/food/files/<year>/<month>/<day>/
{host}:{port}/food/
class FoodViewSet(viewsets.ModelViewSet):
queryset = Food.objects.all()
permission_classes = [blahblah]
authentication_classes = [blahblah]
def list(self, request, *args, **kwargs):
...
return Response(blahblah)
def create(self, request, *args, **kwargs):
...
return Response(blahblah)
@action(['GET'], detail=True)
def files(self, request, year, month, day):
...
return Response(blahblah)
对不起,我通过查询网址清除此问题,如下所示。
{host}:{port}/food/files/?year=2019&month=02&day=27
@action(['GET'], detail=False)
def files(self, request):
year = request.query_params.get('year')
month = request.query_params.get('month')
day = request.query_params.get('day')