gpt4 book ai didi

python - Django 将所有具有不同外键 ID 的值求和,并使用相关表中的字段将它们压缩

转载 作者:行者123 更新时间:2023-11-29 14:02:32 26 4
gpt4 key购买 nike

我想执行类似于 this 的操作(即在付款表中获取不同事件金额的总和,然后按事件详细信息和为他们支付的总金额对付款进行分组。还可以获取用户以及他们为事件支付的费用)在 Django 中使用 PostgreSQL。

我的模型如下:

class UserProfile(User):
onames = models.CharField(max_length=30, blank=True)
phoneNumber = models.CharField(max_length=15, blank=True)
regNo = models.CharField(max_length=15)
designation = models.CharField(max_length=3,choices=DESIGNATION_CHOICES, default='MEM')
image = models.ImageField(max_length=100,upload_to='photos/%Y/%m/%d', blank=True, null=True, default='photos/2010/03/placeholder.jpg')
course = models.CharField(max_length=30, blank=True, null=True)
timezone = models.CharField(max_length=50, default='Africa/Nairobi')
smsCom = models.BooleanField()
mailCom = models.BooleanField()

class Payments(models.Model):
username = models.ForeignKey(UserProfile, related_name='payer')
receiptNo = models.CharField(max_length=30, primary_key=True)
particulars = models.CharField(max_length=50)
date = models.DateField(auto_now_add=True)
amount = models.FloatField(max_length=99, blank=True)
eventID = models.ForeignKey('events', null=True, blank=True)
receiver = models.ForeignKey(UserProfile, related_name='receiver')
deleted = models.BooleanField()

class events(models.Model):
eventName = models.CharField(max_length=100)
eventID = models.AutoField(primary_key=True)
details = models.TextField()
attendanceFee = models.FloatField(max_length=99)
date = models.DateField()
username = models.ManyToManyField(UserProfile, related_name='user')
eventposter = models.ForeignKey(UserProfile, related_name='event_poster')
deleted = models.BooleanField()

最佳答案

经过一些神经破解之后,解决方案是将查询拆分为 2 个,即一个用于分组和求和,另一个用于获取匹配值。

关于分组和求和,引用here

关于python - Django 将所有具有不同外键 ID 的值求和,并使用相关表中的字段将它们压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2534783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com