【问题标题】:how to compare related Sum with a value itself如何将相关 Sum 与值本身进行比较
【发布时间】:2015-10-06 16:56:15
【问题描述】:
class Investor(Model):
    name = CharField(max_length=16)

class Project(Model):
    plan_finance = IntegerField()

class ProjectProcess(Model):
    project = OneToOneField('Project')
    investors = ManyToManyField('Investor')

class InvestShip(Model):
    project = ForeignKey('Project')
    investor = ForeignKey('Investor')
    invest_amount = IntegerField()

如何找到已经融资完成项目,也就是从投资者>plan_finance那里收到的钱。

【问题讨论】:

    标签: django sum many-to-many django-queryset foreign-key-relationship


    【解决方案1】:

    您可以在相关集上使用annotation,然后对其进行过滤。

    from django.db.models import Sum
    
    Project.objects.annotate(invested_sum=Sum('investship_set__invest_amount')).filter(invested_sum__gte=plan_finance)
    

    【讨论】:

    • django.core.exceptions.FieldError:无法将关键字“investship_set”解析为字段。选择是:....(上面是错误消息)
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2023-01-21
    • 2021-01-15
    • 2012-01-28
    • 2021-03-12
    • 2021-04-24
    相关资源
    最近更新 更多