2
我有一個員工模型和一個職位模型。另外,我有很多職位可以在系統中使用(超過2000人)。獲取Django中另一個模型引用的對象
我需要顯示僅在系統中使用的位置。並在Django中使用QuerySet
來忽略其他人。
在SQL我會做這樣的事情。
SELECT
*
FROM
positions
WHERE
position_id IN (SELECT DISTINCT position_id from employees)
在Django中我找不到辦法做到這一點。 與創建員工的名單問題定位
class Employee(models.Model):
first_name = models.CharField(max_length=30, verbose_name=_('first_name'))
position = models.ForeignKey('core.Position', verbose_name=_('Position'))
class Position(models.Model):
title = models.CharField(max_length=30)
#....
# query is to get the positions that are used by employees. Not one employee. ALL employees.
'Position.objects.filter(employee__isnull = False)'對我來說工作很好......謝謝 – Othman 2014-11-21 19:26:03