代码之家  ›  专栏  ›  技术社区  ›  CastleDweller

用django中的布尔值过滤ForeignKey

  •  0
  • CastleDweller  · 技术社区  · 16 年前

    我有这些模型:

    class Client(models.Model):
        is_provider = models.BooleanField()
    
    class Billing(models.Model):
        client = models.ForeignKey(Client)
    

    我想限制ForeignKey的选择只显示具有 is_provider=True . 有没有类似于:

    limit_choices_to = {'is_provider': True}
    

    或者任何我可以用来过滤外键的东西?

    2 回复  |  直到 16 年前
        1
  •  1
  •   Daniel Roseman    16 年前

    client = models.ForeignKey(Client, limit_choices_to = {'is_provider': True})
    
        2
  •  1
  •   muksie    16 年前

    您是否尝试过以下方法:

    limit_choices_to = {'client__is_provider': True}