注释不返回预期结果

时间:2017-05-19 13:12:35

标签: python django django-models annotations django-queryset

我的模型如下:

class Project(models.Model):
    project_id = models.CharField(max_length=10, blank=False, primary_key=True)
    title = models.CharField(max_length=128, blank=False,)
    start_date = models.DateField(null=False)
    end_date = models.DateField(null=True)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2)
    raised_amount = models.DecimalField(max_digits=10, decimal_places=2)
    cause = models.ForeignKey('Cause', on_delete=models.SET('cause not set'))
    ngo_id = models.ForeignKey('NGO', on_delete=models.SET('ngo not set'))
    zip = models.IntegerField(blank = True)
    reimagine_fees=models.DecimalField(max_digits=10, decimal_places=3,default=0.05)
    person_of_contact = models.CharField(max_length = 100)
    summary = models.TextField(blank = False)
    story = models.TextField(blank = True)
    fb_description=models.CharField(max_length = 301,blank=True)
    tax_exemption_available = models.BooleanField(default=False)
    banner = models.TextField(blank=True)
    team_member_id = models.ForeignKey(Team_Member, on_delete=models.SET('team member not set'))
    project_page_desc = models.CharField(max_length=300, blank=True)

    def __str__(self):
        return self.title

我试图根据资金的百分比过滤Project对象,即raised_amounttotal_amount的比例......我正在使用注释。但是,过滤器没有给出我预期给出的结果。我的过滤查询是:

project_list = project_list.annotate(x=F('raised_amount')/F('total_amount')).exclude(x__gte=0.8)

project_list = project_list.annotate(x=F('raised_amount')/F('total_amount')).exclude(x__lte=0.2)

project_list = project_list.annotate(x=F('raised_amount')/F('total_amount')).exclude(x__lte=1, end_date__gte=datetime.date.today())

project_list是所有Project个对象的查询集。

在这里帮助我......

1 个答案:

答案 0 :(得分:0)

projects = Project.objects.all() # projects is a queryset

project_list = projects.annotate(x=F('raised_amount')/F('total_amount')).exclude(x__gte=0.8)

在queryset上尝试所有注释,而不是在字典上。