如果字段存在,Django过滤查询集
Posted
技术标签:
【中文标题】如果字段存在,Django过滤查询集【英文标题】:Django filter queryset if a field exists 【发布时间】:2015-05-06 16:43:30 【问题描述】:当且仅当loan
存在于我的模型ShgGroupLoanMarking
中时,我想过滤我的查询集。
class ShgGroupLoanMarking(models.Model):
shg = models.ForeignKey(Shg, null=True)
category = models.CharField(max_length=25, choices=GROUP_CATEGORY, default="shg_group")
msss = models.ForeignKey(Msss, null=True)
shgmember = models.ForeignKey(ShgMember, null=True)
loan = models.ForeignKey(Loan, null=True)
date_of_marking = models.DateField(null=True)
paid = models.CharField(max_length=100, null=True)
loan_amount = models.CharField(max_length=100, null=True)
service_charge = models.CharField(max_length=100, null=True)
description = models.CharField(max_length=100, null=True)
status = models.CharField(max_length=100, null=True)
print_status = models.BooleanField(default=False)
sent_to_bank_status = models.BooleanField(default=False)
receipt_number = models.IntegerField(default=0)
如何做到这一点?
【问题讨论】:
【参考方案1】:ShgGroupLoanMarking.objects.filter(loan__isnull=False)
或
ShgGroupLoanMarking.objects.exclude(loan=None)
【讨论】:
这实际上并不测试一个字段是否存在,它假定它存在并检查它是否为空。如果该字段确实不存在,这仍然会抛出jango.core.exceptions.FieldError: Cannot resolve keyword '....' into field.
以上是关于如果字段存在,Django过滤查询集的主要内容,如果未能解决你的问题,请参考以下文章