【问题标题】:approaching models from the other side of a foreign key从外键的另一端接近模型
【发布时间】:2010-10-06 10:15:37
【问题描述】:

使用Django docs' 示例BlogEntry 模型,如何获得具有name = "a" 并且与Entry 模型的任何实例无关的所有Blog 对象的查询集?

在原始 (My)SQL 术语中,Django ORM 相当于:

SELECT * FROM blog_table bt
WHERE bt.name='a' AND bt.id NOT IN (SELECT et.blog_id FROM entry_table et)

【问题讨论】:

    标签: django django-orm


    【解决方案1】:

    您需要的是具有name = "a" 并且没有任何关联条目的Blog 实例列表。你可以这样做:

    Blog.objects.filter(name = "a", entry = None)
    #                   ^^^^        ^^^^^
    #           <Match name>        <Should have no associated Entry instances>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 2018-09-21
      • 2017-08-30
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多