Django - filtering on foreign key properties

后端 未结 3 1666
情书的邮戳
情书的邮戳 2020-11-27 12:14

I\'m trying to filter a table in Django based on the value of a particular field of a ForeignKey.

For example, I have two models:

class          


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 13:09

    This has been possible since the queryset-refactor branch landed pre-1.0. Ticket 4088 exposed the problem. This should work:

    Asset.objects.filter(
        desc__contains=filter,
        project__name__contains="Foo").order_by("desc")
    

    The Django Many-to-one documentation has this and other examples of following Foreign Keys using the Model API.

提交回复
热议问题