开发者

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

开发者 https://www.devze.com 2022-12-24 18:24 出处:网络
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 an开发者_如何学Cd total money paid for them. Als

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 an开发者_如何学Cd total money paid for them. Also getting users and what they have paid for an event will be done) in Django using 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, i.e, one for grouping and summing and the other one for getting the matching values.

For grouping and summing, refer here

0

精彩评论

暂无评论...
验证码 换一张
取 消