python - How do we define @list_route that accept arguments -
in application have modelviewset 1 @list_route() defined function getting list different serializer.
class animalviewset(viewsets.modelviewset): """ viewset automatically provides `list`, `create`, `retrieve`, `update` , `destroy` actions. """ queryset = animal.objects.all() serializer_class = animalserializer // default modelviewset serializer lookup_field = 'this_id' @list_route() def listview(self, request): query_set = animal.objects.all() serializer = animallistingserializer(query_set, many=true) // serializer different field included. return response(serializer.data) the default animalviewset /api/animal/ end point yield serialized data result based on animalserializer definition.
{ "this_id": "1001", "name": "animal testing 1", "species_type": "cow", "breed": "brahman", ... "herd": 1 }, { "this_id": "1004", "name": "animal testing 2", "species_type": "cow", "breed": "holstien", .... "herd": 1 }, { "this_id": "1020", "name": "animal testing 20", "species_type": "cow", "breed": "brahman", .... "herd": 4 }, and other 1 @list_route() defined function named listview may have end point /api/animal/listview/ yields result defined in animallistingserializer structure.
{ "this_id": "1001", "name": "animal testing 1", "species_type": "cow", "breed": "brahman", .... "herd": { "id": 1, "name": "high production", "description": null } }, { "this_id": "1004", "name": "animal testing 2", "species_type": "cow", "breed": "holstien", .... "herd": { "id": 1, "name": "high production", "description": null } }, { "this_id": "1020", "name": "animal testing 20", "species_type": "cow", "breed": "brahman", .... "herd": { "id": 4, "name": "bad production", "description": "bad production" } } now trying want define @list_route() function takes argument , uses animallistingserializer in order filter query_set result of model object. work around beginner us.
@list_route() def customlist(self, request, args1, args2): query_set = animal.objects.filter(species_type=args1, breed=args2) serializer = animallistingserializer(query_set, many=true) return response(serializer.data) let assumed args1 = "cow" , args2 = "brahman". , expecting result.
{ "this_id": "1001", "name": "animal testing 1", "species_type": "cow", "breed": "brahman", .... "herd": { "id": 1, "name": "high production", "description": null } }, { "this_id": "1020", "name": "animal testing 20", "species_type": "cow", "breed": "brahman", .... "herd": { "id": 4, "name": "bad production", "description": "bad production" } }, but know syntax wrong, talking about. please help.
parameters in view function reserved url references. ie route animals/5 passed view function has pk argument.
def get(self, request, pk): # animal pk return animal pk by decorating @list_route on ride option provide additional params via url. in case resource can request via url listview; /animals/listview. have other options though if want continue down path. can pass parameters url via url, query param /animals/listview/?speceis_type=cow&breed=braham access in view via request.query_params['speceis_type'] , request.query_params['braham'] or can use django rest filter middleware documented here
Comments
Post a Comment