how to sum model in Django rest Api

Question:

I am new to Django rest API development I want to sum rent_amount, bijli_bill, other_amount and get value as Total amount, I don’t know to add them pls help

I want value like this

{
"id": 1,
"rent_date": "23-08-2022",
"rentmonth": "June",
"rent_amount": 500.0,
"bijli_bill": 200.0,
"other_amount": 100.0,
"other_commnet": "test",
"total_amount": 800.0,
}

This the model file

from sqlite3 import Date from django.db import models from django
import forms from django.contrib.auth.models import User

class rent(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    # id=models.IntegerField(primary_key=True)
    rent_date = models.DateField(auto_now_add=True)
    rentmonth = models.CharField(max_length=30)
    rent_amount = models.FloatField()
    bijli_bill = models.FloatField()
    other_amount = models.FloatField(blank=True)
    other_commnet = models.CharField(blank=True, max_length=200)

This is my serializer

from rest_framework import serializers from .models import rent from
django.contrib.auth.models import User from django.db.models import
Sum

class rentSerializer(serializers.ModelSerializer):
    rent_date = serializers.DateField(format="%d-%m-%Y", read_only=True)
    rentmonth = serializers.CharField()
    rent_amount = serializers.FloatField()
    bijli_bill = serializers.FloatField()
    other_amount = serializers.FloatField()
    other_commnet = serializers.CharField(max_length=200)
 

    class Meta:
        model = rent
        fields = ('__all__')

This is view file

class rentViews(APIView):

def get(self, request, id=None):
    if id:
        item = rent.objects.get(id=id)
        serializer = rentSerializer(item)
        return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)

    items = rent.objects.all()
    serializer = rentSerializer(items, many=True)
    return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
Asked By: Punit kumar

||

Answers:

You can create a custom Property to your Django model that is called total_cost for example

class rent(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    rent_date = models.DateField(auto_now_add=True)
    rentmonth = models.CharField(max_length=30)
    rent_amount = models.FloatField()
    bijli_bill = models.FloatField()
    other_amount = models.FloatField(blank=True)
    other_commnet = models.CharField(blank=True, max_length=200)

    @property
    def total_cost(self):
        return self.rent_amount + self.bijli_bill + self.other_amount

now you can get total_cost like any other variable

rentExample = rent.objects.create(User=user, rentmonth="02", rent_amount=30, bijli_bill=30, other_amount=30)

print(rentExample.total_cost)
# output: 90
Answered By: MrKioZ
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.