django FilteredRelation 的用法

有两大用处

  • FilteredRelation类允许为查询集增加一个ON从句
  • 可以在多个join中语句直接引用变量
from django.db.models import FilteredRelation, Q

result_1 = Restaurant.objects.annotate(pizzas_vegetarian=FilteredRelation('pizzas', condition=Q(pizzas__vegetarian=True), ), ).filter(
    pizzas_vegetarian__name__icontains='mozzarella')
posted @ 2023-01-19 09:31  vx_guanchaoguo0  阅读(97)  评论(0编辑  收藏  举报