Django动态过滤失败
作为这个问题的后续,我想查明实际发生的错误。我做错了什么,还是这是一个错误?
f = {'groups__isnull': 'True'}
students1 = models.Student.objects.filter( **f )
students2 = models.Student.objects.filter(groups__isnull=True)
这两个查询应该相同,但事实并非如此。
作为参考,我的模型:
class Student (models.Model):
user = models.ForeignKey(User, unique=True, null=False, related_name='student')
teacher = models.ForeignKey(User, null=False, related_name='students')
assignment = models.ForeignKey(LabJournal, blank=True, null=True, related_name='students')
class JournalGroup (models.Model):
title = models.CharField(null=False, max_length=256)
owner = models.ForeignKey(User, null=True, related_name='journal_groups')
members = models.ManyToManyField(Student, blank=True, related_name='groups')
As a follow-up to this question, I'd like to pinpoint the actual error that was occurring. Am I doing something wrong, or is this a bug?
f = {'groups__isnull': 'True'}
students1 = models.Student.objects.filter( **f )
students2 = models.Student.objects.filter(groups__isnull=True)
These two queries should be identical, but are not.
For reference, my models:
class Student (models.Model):
user = models.ForeignKey(User, unique=True, null=False, related_name='student')
teacher = models.ForeignKey(User, null=False, related_name='students')
assignment = models.ForeignKey(LabJournal, blank=True, null=True, related_name='students')
class JournalGroup (models.Model):
title = models.CharField(null=False, max_length=256)
owner = models.ForeignKey(User, null=True, related_name='journal_groups')
members = models.ManyToManyField(Student, blank=True, related_name='groups')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现查询之间存在明显差异。
{'groups__isnull': True}
永远不等于{'groups__isnull': 'True'}
。一种将 True 作为布尔值提供,另一种将 True 作为字符串提供。
I see an obvious difference between queries.
{'groups__isnull': True}
is never equal to{'groups__isnull': 'True'}
.One provides True as boolean, other as a string.