<code>class Transactions(models.Model):
id = models.CharField(max_length=100, primary_key=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
date = models.DateField()
watch = models.CharField(max_length=100)
purchase_price = models.IntegerField()
sale_price = models.IntegerField()
</code>
<code>class Transactions(models.Model):
id = models.CharField(max_length=100, primary_key=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
date = models.DateField()
watch = models.CharField(max_length=100)
purchase_price = models.IntegerField()
sale_price = models.IntegerField()
</code>
class Transactions(models.Model):
id = models.CharField(max_length=100, primary_key=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
date = models.DateField()
watch = models.CharField(max_length=100)
purchase_price = models.IntegerField()
sale_price = models.IntegerField()
My Transactions model has a date field, purchase price, and sale_price.
<code>@api_view(['GET'])
@permission_classes([IsAuthenticatedOrReadOnly])
def profit_chart(request):
current_year = datetime.now().year
queryset = Transactions.objects.filter(date__year=current_year).values(month=ExtractMonth('date'),
year=ExtractYear('date')).annotate(
profit=Sum(F('sale_price')) - Sum(F('purchase_price'))).order_by('month')
serializer = ProfitChartSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
</code>
<code>@api_view(['GET'])
@permission_classes([IsAuthenticatedOrReadOnly])
def profit_chart(request):
current_year = datetime.now().year
queryset = Transactions.objects.filter(date__year=current_year).values(month=ExtractMonth('date'),
year=ExtractYear('date')).annotate(
profit=Sum(F('sale_price')) - Sum(F('purchase_price'))).order_by('month')
serializer = ProfitChartSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
</code>
@api_view(['GET'])
@permission_classes([IsAuthenticatedOrReadOnly])
def profit_chart(request):
current_year = datetime.now().year
queryset = Transactions.objects.filter(date__year=current_year).values(month=ExtractMonth('date'),
year=ExtractYear('date')).annotate(
profit=Sum(F('sale_price')) - Sum(F('purchase_price'))).order_by('month')
serializer = ProfitChartSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
My view sorts transactions by the current year and month and then calculates the profit for that month.
<code>class ProfitChartSerializer(serializers.ModelSerializer):
year = serializers.IntegerField()
month = serializers.IntegerField()
profit = serializers.IntegerField()
class Meta:
model = Transactions
fields = ['year', 'month', 'profit']
</code>
<code>class ProfitChartSerializer(serializers.ModelSerializer):
year = serializers.IntegerField()
month = serializers.IntegerField()
profit = serializers.IntegerField()
class Meta:
model = Transactions
fields = ['year', 'month', 'profit']
</code>
class ProfitChartSerializer(serializers.ModelSerializer):
year = serializers.IntegerField()
month = serializers.IntegerField()
profit = serializers.IntegerField()
class Meta:
model = Transactions
fields = ['year', 'month', 'profit']
The response I get from DRF is below.
<code>[
{
"year": 2024,
"month": 8,
"profit": 5000
}
]
</code>
<code>[
{
"year": 2024,
"month": 8,
"profit": 5000
}
]
</code>
[
{
"year": 2024,
"month": 8,
"profit": 5000
}
]
The question I am trying to figure out is how I can manipulate the above response to look like below.
<code>[
year: 2024
data: {
month: 8
profit: 5000
}
]
</code>
<code>[
year: 2024
data: {
month: 8
profit: 5000
}
]
</code>
[
year: 2024
data: {
month: 8
profit: 5000
}
]