LEFT JOIN Django ORM

前端 未结 4 1917
天命终不由人
天命终不由人 2020-11-28 07:30

I have the following models:

class Volunteer(models.Model):
    first_name = models.CharField(max_length=50L)
    last_name = models.CharField(max_length=50L         


        
4条回答
  •  攒了一身酷
    2020-11-28 08:21

    You can do this by following the backwards relation in the lookup.

    >>> qs = Department.objects.filter(departmentvolunteer__isnull=True).values_list('name', flat=True)
    >>> print(qs.query)
    SELECT "app_department"."name" FROM "app_department" LEFT OUTER JOIN
    "app_departmentvolunteer" ON ( "app_department"."id" = "app_departmentvolunteer"."department_id" )
    WHERE "app_epartmentvolunteer"."id" IS NULL
    

    Here are the docs on queries "Spanning multi-valued relationships": https://docs.djangoproject.com/en/stable/topics/db/queries/#spanning-multi-valued-relationships

提交回复
热议问题