Django动态过滤器故障 [英] Django dynamic filter failure

查看:65
本文介绍了Django动态过滤器故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的模型仅供参考:

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')


推荐答案

我发现查询之间有明显的区别。

I see an obvious difference between queries.

{'groups__isnull':True } 永远不等于 {'groups__isnull':'True'}

一个提供True为布尔值,另一个提供为字符串。

One provides True as boolean, other as a string.

这篇关于Django动态过滤器故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆