I'm Trying to add the sum of multiple objects to a DRF response. For example, right now the response works with just listing the objects:
[
{
"id": "47d0deaa5c8c",
"amount": "25.00"
},
{
"id": "29787731",
"amount": "25.00"
}
]
But what I want is to be able to sum the amount attribute of those objects together and then include that in the response, so that it would look like this:
{
"sum":"50.00",
"objects":[
{
"id":"47d0deaa5c8c",
"amount":"25.00"
},
{
"id":"29787731",
"amount":"25.00"
}
]
}
Here's my current APIView:
class TransactionsList(GenericAPIView):
"""
Retrieve list of transactions
"""
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def get(self, request):
"""List Transactions"""
transaction = Transaction.objects.all()
serializer = TransactionSerializer(transaction, many=True)
return Response(serializer.data)
And Serializer:
class TransactionSerializer(serializers.ModelSerializer):
class Meta:
model = Transaction
fields = ('id', 'amount')
How could I efficiently go about adding the sum field into the response?
change
return Response(serializer.data)
to
from django.db.models import Sum
all_sum = transaction.aggregate(Sum('amount'))['amount__sum']
return Response({'sum': all_sum if all_sum else 0 , 'objects': serializer.data})
Since the amount field seems string type. So, we've to convert to int before summing them.
class TransactionsList(GenericAPIView):
"""
Retrieve list of transactions
"""
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def get(self, request):
"""List Transactions"""
transaction = Transaction.objects.all()
serializer = TransactionSerializer(transaction, many=True)
return_data = {"sum": str(sum([lambda items: int(items['amount'])])), "objects": serializer.data}
return Response(return_data)
If the amount field is int type, then you don't want to add do the type casting. If so, use this,
return_data = {"sum": sum([lambda items: items['amount']]), "objects": serializer.data}
Related
Can someone advise me on how to deal with how to retrieve data such as this example
JSON:
[
{
"deviceid": 4,
"devicename": "###",
"device_measurment": {
"deviceid": 4,
"measurement": "31.8",
}
},
]
How is it right now:
[
{
"deviceid": 4,
"devicename": "###",
},
{
"deviceid": 4,
"measurement": "31.8",
},
]
I understand the problem and why I am getting such a response, but I cannot handle this problem via the serializer. at the moment, I have the following
Views
from itertools import chain
class RetrieveSpecificDevicesView(APIView):
permission_classes = [permissions.IsAuthenticated]
serializer_class = SpecificDeviceSerializer
def post(self, request):
if self.request.method == 'POST':
serializer = SpecificDeviceSerializer(data=request.data)
if serializer.is_valid():
obj1= Devices.objects.using('###').filter(deviceid=serializer.data.get("deviceid"))
obj2= Measurements.objects.using('###').filter(deviceid=serializer.data.get("deviceid"))
dats= chain(obj1, obj2)
data = serializers.serialize('json', dats)
return HttpResponse(data, content_type="application/json")
return JsonResponse(serializer.erorr, status=status.HTTP_201_CREATED)
Serializer
class SpecificDeviceSerializer(serializers.Serializer):
deviceid = serializers.CharField(required=True)
class MeasurmentsSerializer(serializers.ModelSerializer):
class Meta:
model = Measurements
fields = ('measurementid','measurement')
Models
enter image description here
Update:
I am trying to implement via serializer but something is going wrong:
Note: By wrong I mean => *Got AttributeError when attempting to get a value for field devicesname on serializer SpecificDeviceSerializer*.
Views
class RetrieveSpecificDevicesView(APIView):
permission_classes = [permissions.IsAuthenticated]
serializer_class = SpecificcDeviceSerializer
def post(self, request):
if self.request.method == 'POST':
data = self.serializer_class.get_device(self, obj=request.data)
user = SpecificDeviceSerializer(data)
return HttpResponse(user)
Serializers
class SpecificDeviceSerializer(serializers.ModelSerializer):
class Meta:
model = Devices
fields = ( 'deviceid', 'devicename')
class SpecificcDeviceSerializer(serializers.ModelSerializer):
def get_device(self, obj):
device = Devices.objects.using('###').filter(deviceid=obj.get("deviceid"))
return device
class Meta:
model = Devices
fields = ( 'deviceid', 'devicename')
Solution:
Serializer
class SpecificDeviceSerializer(serializers.ModelSerializer):
measurments = serializers.SerializerMethodField()
def get_measurments(self, obj):
device_measurements = obj.measurment_details.last()
serializer = MeasurmentsSerializer(device_measurements)
return serializer.data
class Meta:
model = Devices
fields = ('devicename','measurments')
Views
class RetrieveSpecificDevicesView(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = SpecificDeviceSerializer
def get_queryset(self):
return Devices.objects.using('###').filter(deviceid=self.request.data.get("deviceid"))
JSON:
[
{
"devicename": "RTMU 3068",
"measurments": {
"measurementid": 2980465,
"measurement": "25.6"
}
}
]
Documentation: Best for this case
Parent id or foreign key is not returning from child. I am trying drf-nested-routers example.
models.py
class Client(models.Model):
name = models.CharField(max_length=255)
class MailDrop(models.Model):
title = models.CharField(max_length=255)
client_id = models.ForeignKey(Client, on_delete=models.CASCADE)
serializers.py
class ClientSerializer(HyperlinkedModelSerializer):
class Meta:
model = Client
fields = ['id', 'name']
class MailDropSerializer(NestedHyperlinkedModelSerializer):
parent_lookup_kwargs = {
'client_pk': 'client_id',
}
class Meta:
model = MailDrop
fields = ['id', 'title']
views.py
class ClientViewSet(viewsets.ViewSet):
serializer_class = ClientSerializer
def list(self, request):
queryset = Client.objects.filter()
serializer = ClientSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
queryset = Client.objects.filter()
client = get_object_or_404(queryset, pk=pk)
serializer = ClientSerializer(client)
return Response(serializer.data)
class MailDropViewSet(viewsets.ViewSet):
serializer_class = MailDropSerializer
def list(self, request, client_pk=None):
queryset = MailDrop.objects.filter(client_id=client_pk)
serializer = MailDropSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None, client_pk=None):
queryset = MailDrop.objects.filter(pk=pk, client_id=client_pk)
maildrop = get_object_or_404(queryset, pk=pk)
serializer = MailDropSerializer(maildrop)
return Response(serializer.data)
If I add fields = ['id', 'title', 'client_id'] in the MailDropSerializer, it throws the following error:
AssertionError: `NestedHyperlinkedRelatedField` requires the request in the serializer context. Add `con
text={'request': request}` when instantiating the serializer.
If I add the request in the serializer context, the output is as follows:
url: http://127.0.0.1:8000/clients/4/maildrops/
[
{
"id": 3,
"title": "Madison Mosley",
"client_id": "http://127.0.0.1:8000/clients/4/"
},
{
"id": 4,
"title": "Louis Chen",
"client_id": "http://127.0.0.1:8000/clients/4/"
}
]
I added the serializer context as follows:
def list(self, request, client_pk=None):
queryset = MailDrop.objects.filter(client_id=client_pk)
serializer_context = {
'request': request
}
serializer = MailDropSerializer(
queryset, many=True, context=serializer_context)
return Response(serializer.data)
I am trying to get something as follows:
[
{
"id": 3,
"title": "Madison Mosley",
"client_id": 4
},
{
"id": 4,
"title": "Louis Chen",
"client_id": 4
}
]
Just solved:
class MailDropSerializer(NestedHyperlinkedModelSerializer):
parent_lookup_kwargs = {
'client_pk': 'client_id',
}
client_id = IntegerField(source='client_id.id')
class Meta:
model = MailDrop
fields = ['id', 'title', 'client_id']
I have 3 different models. I need to build a custom serializer with these 3 models. I tried using some code, but it didn't reach what I expected.
There is structure, but I was not able to get the data I was expecting
# Serializer
This my Serializer and ModelOne, ModelTwo, ModelThree these are my models.
class ModelOneSerializer(serializers.ModelSerializer):
class Meta:
model = ModelOne
fields = ['id', 'obj_1', 'obj_2']
class ModelTwoSerializer(serializers.ModelSerializer):
class Meta:
model = ModelTwo
fields = ['id', 'obj_1', 'obj_2']
class ModelThreeSerializer(WritableNestedModelSerializer):
class Meta:
model = ModelThree
fields = ['id', 'obj_1', 'obj_2']
class CustomSerializer(serializers.Serializer):
model_1 = ModelOneSerializer(many=True)
model_2 = ModelTwoSerializer(many=True)
model_3 = ModelThreeSerializer(many=True)
# View
class CustomView(APIView):
def get(self, request, *args, **kwargs):
serializer = CustomSerializer(context={'request': request})
return Response({'response': 'ok', 'result': serializer.data})
# Expected Output
{
"response": "ok",
"result": {
"model_1": [
{
"id":"1",
"obj_1":"test",
"obj_2":"test",
"obj_3":"test"
},
{
"id":"1",
"obj_1":"test",
"obj_2":"test",
"obj_3":"test"
}
],
"model_2": [
{
"id":"1",
"obj_1":"test",
"obj_2":"test",
"obj_3":"test"
}
],
"model_3": [
{
"id":"1",
"obj_1":"test",
"obj_2":"test",
"obj_3":"test"
}
]
}
}
The results may have multiple data in the model_2 and model_3 as in the model_1 structure
You didn't pass what to serialize for all the model serializers, so CustomSerializer doesn't have anything to serialize against.
You can pass the querysets of the models to CustomSerializer:
class CustomView(APIView):
def get(self, request, *args, **kwargs):
model_one_qset = ModelOne.objects.all()
model_two_qset = ModelTwo.objects.all()
model_three_qset = ModelThree.objects.all()
qsets_map = {
'model_1': model_one_qset,
'model_2': model_two_qset,
'model_3': model_three_qset,
}
serializer = CustomSerializer(qsets_map, context={'request': request})
return Response({'response': 'ok', 'result': serializer.data})
With a serializer like this...
class MyPriceSerializer(serializers.Serializer):
prices = serializers.SerializerMethodField()
def get_prices(self, obj):
return obj.prices.values_list('price', flat=True)
class ChartData(RetrieveAPIView):
queryset = Market.objects.all()
authentication_classes = []
permission_classes = []
serializer_class = MyPriceSerializer
...I can pick up a flat list like this on the other end:
{
"prices": [
0.52,
0.55,
...
0.94,
0.93
]
}
And if I change the serlializer to return obj.prices.values_list('price_date', flat=True), I can get this:
{
"prices": [
"2019-07-22T15:19:02.924811Z",
"2019-07-22T15:19:02.924811Z",
...
"2019-07-22T15:58:41.337422Z",
"2019-07-22T16:04:16.753870Z"
]
}
But is it possible to get both (as below) in one serializer, so that I can pick up the result in one Ajax call in a template?
{
"prices": [
0.52,
0.55,
...
0.94,
0.93
]
"price_dates": [
"2019-07-22T15:19:02.924811Z",
"2019-07-22T15:19:02.924811Z",
...
"2019-07-22T15:58:41.337422Z",
"2019-07-22T16:04:16.753870Z"
]
}
You can just add another SerializerMethodField:
class MyPriceSerializer(serializers.Serializer):
prices = serializers.SerializerMethodField()
price_dates = serializers.SerializerMethodField()
def get_prices(self, obj):
return obj.prices.values_list('price', flat=True)
def get_price_dates(self, obj):
return obj.prices.values_list('price_date', flat=True)
If you add prefetch_related to queryset this allows you to add as many field from prices as you want without extra sql query:
class ChartData(RetrieveAPIView):
queryset = Market.objects.all().prefetch_related('prices')
authentication_classes = []
permission_classes = []
serializer_class = MyPriceSerializer
I am working with Django Rest Framework by firt time and now I'm trying to get an output like this:
{
"qty": 5,
"total": 20,
"items": [
{
"id": 1,
"name": "name_1"
},
{
"id": 2,
"name": "name_2"
}
]
}
from a Serializer. The result data in output above, came from a queryset. I'd like to work with the queryset inside the serializer class. I've not been able to get results as I want without makeing queries inside the serializer:
class ResSerializer(serializers.Serializer):
qty = serializers.SerializerMethodField()
items = serializers.SerializerMethodField()
total = serializers.SerializerMethodField()
def get_qty(self, obj):
try:
return Model.objects.filter(...)\
.aggregate(qty=Sum('job__long'))\
.get('qty')
except KeyError:
return 0
def get_items(self, obj):
print 'testing'
def get_total(self, obj):
return 0
class Meta:
fields = ('qty', 'items', 'total')
I'm calling Serializer like this:
queryset = Model.objects.filter(...)
serialized = ResSerializer(queryset, many=False, context={'current_user': request.user})
But this is not working as I want. Any sugestion? Thanks.
UPDATE
This is the model I query to:
class Intermediate(models.Model):
partner = models.ForeignKey('partner.Partner')
job = models.ForeignKey(Job)
joined_at = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
status = models.SmallIntegerField(default=STATUS_ACCEPTED)
reason_index = models.SmallIntegerField('Cancel reason', default=REASON_3)
start_time = models.TimeField(null=True)
end_time = models.TimeField(null=True)
start_date = models.DateField(null=True)
end_date = models.DateField(null=True)
And here's the view:
class ResView(CustomAPIView):
authentication_classes = (CustomTokenAuthentication, )
# permission_classes = (PartnerAuthenticatedOnly, ) # Uncomment this on server
def post(self, request, *args, **kwargs):
try:
queryset = JobPartner.objects.filter(...)
serialized = ResSerializer(queryset, many=False, context={'current_user': request.user})
response_success_object(self.response_dic, serialized.data)
return Response(self.response_dic)
except Exception, e:
print e
To get the items representation, you can use ItemsSerializer which will give the serialized data having id and name.
class ItemsSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel # specify your model
fields = ('id', 'name') # return these 2 fields in the representation
This serializer when dealing with multiple instances will return the serialized data in below fashion.
[
{
"id": 1,
"name": "name_1"
},
{
"id": 2,
"name": "name_2"
}
]
Now, the qty and total fields depends on the queryset and not a particular object of the queryset, it would be better if you compute them separately in your view. Then create a dictionary containing the fields items, qty and total and return it as the response.
class ResView(CustomAPIView):
authentication_classes = (CustomTokenAuthentication, )
# permission_classes = (PartnerAuthenticatedOnly, ) # Uncomment this on server
def post(self, request, *args, **kwargs):
try:
queryset = JobPartner.objects.filter(...)
qty = self.get_qty() # compute the value of qty
total = self.get_total() # compute the value of total
items_serializer = ItemsSerializer(queryset, many=True)
items = items_serializer.data # compute the value of items
return_dict = { # prepare response data
'qty' : qty,
'total': total,
'items': items
}
return Response(return_dict) # return the response
except Exception, e:
print e