简体   繁体   English

Django将具有唯一外键ID的所有值相加,并用相关表中的字段压缩它们

[英]Django Sum all values with a distinct ForeignKey ID & zip them with fields from related table

I would like to perform something similar to this (ie get the sum of distinct event amounts in a payment table then group the payments by event details and total money paid for them. Also getting users and what they have paid for an event will be done) in Django using PostgreSQL. 我想执行与此类似的操作(即,在付款表中获得不同事件金额的总和,然后按事件明细和为其支付的总金额对付款进行分组。还将获得用户及其为事件支付的金额)在Django中使用PostgreSQL。

My models are as below: 我的模型如下:

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()

after some nerve cracking, the solution was to split the query into 2, ie, one for grouping and summing and the other one for getting the matching values. 经过一阵神经nerve,解决方案是将查询分为2个,即一个用于分组和求和,另一个用于获取匹配值。

For grouping and summing, refer here 有关分组和求和,请参见此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用'select_related'接收来自related(ForeignKey)Django模型的所有字段 - How to recieve not all fields from related(ForeignKey) django model using 'select_related' Django Rest框架,如何使用foreignkey_id字段包含“ __all__”字段和相关字段 - Django Rest framework, how to include '__all__' fields and a related field using foreignkey_id field Django queryset:检查外键值并从queryset中减去它们 - Django queryset: check for foreignkey values and substract them from queryset 如何总结Django模板中ForeignKey对象的所有整数属性? - How to sum all integer properties from ForeignKey objects in django template? Django:过滤与ForeignKey对象相关的所有操作 - Django: Filter for all actions related to the ForeignKey Object 如何获取此 Django REST 查询以显示所有字段,包括相关一对一表中的字段 - How to get this Django REST query to display all fields, including fields from related one-to-one table 从Django QuerySet获取所有值以及相关模型中的其他字段 - Get all values from Django QuerySet plus additional fields from a related model Django 迭代 zip 中的所有值 - Django iterate all values from zip 使用 Django 中相关 model 中的两个字段的总和来注释查询 - Annotating query with sum of multiplication of two fields from a related model in Django 如何在另一个表Django中引用外键字段 - How references fields that are foreignkey in another table , Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM