Django动态过滤失败

发布于 2024-11-02 20:17:37 字数 932 浏览 1 评论 0原文

作为这个问题的后续,我想查明实际发生的错误。我做错了什么,还是这是一个错误?

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2024-11-09 20:17:37

我发现查询之间存在明显差异。

{'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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文