Django REST Framework Customized Json Data Format -
i, started using django rest framework few days ago, it's wonderful framework, can't find info this: serialized model "deposito" , data in json format succesfully, want add fields total (total records).
thanks help.
data obtained serializers.modelserializer
[{ "id": 78, "numero": "2014051100001", "monto": "100.00", "ingreso": "2014-08-13t22:30:00z", "entregado": true }, { "id": 533, "numero": "2014051100221", "monto": "200.00", "ingreso": "2014-08-22t14:45:00z", "entregado": true }]
data want get
{ "rows": [{ "id": 78, "numero": "2014051100001", "monto": "100.00", "ingreso": "2014-08-13t22:30:00z", "entregado": true }, { "id": 533, "numero": "2014051100221", "monto": "200.00", "ingreso": "2014-08-22t14:45:00z", "entregado": true }] "total": 2 }
here code
# serializer.py class depositosserializer(serializers.modelserializer): class meta: model = deposito fields = ('id', 'numero', 'monto', 'ingreso', 'entregado') #views.py def depositobyclient(request, cliente): """ list deposito cliente. """ try: deposito = deposito.objects.filter(cliente=cliente, entregado = true).order_by('numero') except deposito.doesnotexist: return httpresponse(status=400) if request.method == 'get': serializer = depositosserializer(deposito) return jsonresponse(serializer.data)
what's adding total value data before returning response?
#views.py def depositobyclient(request, cliente): """ list deposito cliente. """ try: deposito = deposito.objects.filter(cliente=cliente, entregado = true).order_by('numero') except deposito.doesnotexist: return httpresponse(status=400) if request.method == 'get': serializer = depositosserializer(deposito) rows = serializer.data total = # whatever want here ... return jsonresponse(dict(rows=rows, total=total))
Comments
Post a Comment