Django dynamic filter failure

好久不见. 提交于 2019-12-11 15:24:36

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/5762328/django-dynamic-filter-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!