Saturday, March 27, 2021

how to use reverse query data from a child model to do arithmetic's in parent model?

from django.db import models


class location(models.Model):
name=models.CharField(max_length=100)
total_amount_spent=models.FloatField(default=0)
def __str__(self):
return self.name

class transaction(models.Model):
location_name=models.ForeignKey(location,on_delete=models.CASCADE,null=True)
date_time=models.DateTimeField(null=True)
amount=models.FloatField(null=True)
def __int__(self):
return self.id









I have created a model location, and a model transaction which has a many to one relation with location which has the information of a transaction done at a particular location, say 3 dollars spent at macdonalds, now I want to find all the transactions related to a particular location and add the amount to find the total amount spent,please help

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9a3cba46-c475-49f1-9011-74dd037de8c8n%40googlegroups.com.

No comments:

Post a Comment