clker
V2EX  ›  Django

请教一个 django 的 query 问题

  •  
  •   clker ·
    clker · Jun 2, 2014 · 3524 views
    This topic created in 4443 days ago, the information mentioned may be changed or developed.
    场景:
    Class A:
    pass

    class B:
    f1 = models.ManyToManyField('A',related_name='b')
    f2 = models.BooleanField(default=False)

    表里面有一个A instance a, 两个B instance 均有f1对应于a

    查询:
    A.objects.filter(b__f2 = 0)

    结果返回了queryset [a,a],两个a instance。

    这是怎么回事啊?正确的写法是什么呢?
    3 replies    2014-06-02 19:34:32 +08:00
    wenbinwu
        1
    wenbinwu  
       Jun 2, 2014   ❤️ 1
    print A.objects.filter(b__f2 = 0).query
    看一下SQL就明白了:)
    ericFork
        2
    ericFork  
       Jun 2, 2014   ❤️ 1
    LS+1,workaround 是加上 .distinct()
    clker
        3
    clker  
    OP
       Jun 2, 2014
    @wenbinwu
    @ericFork

    感谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2735 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 241ms · UTC 09:59 · PVG 17:59 · LAX 02:59 · JFK 05:59
    ♥ Do have faith in what you're doing.