我有以下代码:
class Skill(models.Model):
name = models.CharField(max_length=60)
class Competency(models.Model):
skill = models.ForeignKey(Skill, null=True, on_delete=CASCADE)
class Employee(AbstractUser):
competencies = models.ManyToManyField(Competency, blank=True, related_name='employees')
我想从特定技能中获取具有该技能能力的员工的查询集。
通过该技能,我可以获得以下相关能力的列表:
skill = Skill.objects.get(pk=id)
competencies = skill.competency_set.all()
鉴于“能力”包含许多要素,如何创建员工查询集?
答案 0 :(得分:2)
与往常一样,从您要获得的模型开始:员工。
skilled_employees = Employee.objects.filter(competencies__skill=skill)
或者只是
skilled_employees = Employee.objects.filter(competencies__skill_id=id)