samedi 29 novembre 2014

Android - Framework Django reste : afficher directement sur la liste des résultats en GenericView - Stack Overflow


I'm using djangorestframework to manage a REST API that connects an Android mobile app to my Django web application. I have a list of objects that I need to retrieve from the web app through the REST API, so far a generic ListCreateAPIView works fine.


However, what the REST API returns isn't exactly a list/array per se, but a JSON object containing metadata and the actual results list. Here's an example of the said output:


{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"foo":"bar"
}
]
}

The problem is, my mobile app's REST client expects a JSON list/array, not the JSON object above. Is there a way to make my generic view remove the count, next, and previous metadata and just output the results list itself? I need an output like the following:


[
{"foo":"bar"},
{"foo":"something"},
{"foo":"another"}
]

Oh, and I'm not sure if this would be helpful, but I use Retrofit as a REST client for my Android app, which is supposed to connect to my web app's REST API.




This object that wraps the array is generated by the queryset paginator. If you disable pagination you will get the array. To disable pagination, set paginate_by to None:


class PaginatedListView(ListAPIView):
queryset = ExampleModel.objects.all()
serializer_class = ExampleModelSerializer
paginate_by = None


I'm using djangorestframework to manage a REST API that connects an Android mobile app to my Django web application. I have a list of objects that I need to retrieve from the web app through the REST API, so far a generic ListCreateAPIView works fine.


However, what the REST API returns isn't exactly a list/array per se, but a JSON object containing metadata and the actual results list. Here's an example of the said output:


{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"foo":"bar"
}
]
}

The problem is, my mobile app's REST client expects a JSON list/array, not the JSON object above. Is there a way to make my generic view remove the count, next, and previous metadata and just output the results list itself? I need an output like the following:


[
{"foo":"bar"},
{"foo":"something"},
{"foo":"another"}
]

Oh, and I'm not sure if this would be helpful, but I use Retrofit as a REST client for my Android app, which is supposed to connect to my web app's REST API.



This object that wraps the array is generated by the queryset paginator. If you disable pagination you will get the array. To disable pagination, set paginate_by to None:


class PaginatedListView(ListAPIView):
queryset = ExampleModel.objects.all()
serializer_class = ExampleModelSerializer
paginate_by = None

0 commentaires:

Enregistrer un commentaire