Reputation: 1402
I have 3 models:
completed
: Indicate whether the employee
has done the assigned task yet.class Task(models.Model):
name = models.CharField(max_length=50)
detail = models.TextField()
employees = models.ManyToManyField('Employee', through='AssignedUser', related_name='tasks')
class AssignedUser(models.Model):
task = models.ForeignKey(task, on_delete=models.CASCADE)
employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
completed = models.BooleanField(default=False)
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.ForeignKey(to=Department, on_delete=models.CASCADE, null=False)
# Note - department field is not relevant to this question.
If I want to select a task name and the assigned employee's username who hasn't done the task yet (completed=False
) how can I do that with Django's ORM?
Here's the equivalent SQL I wrote for the desired result which I have no idea how to achieve that with Django's ORM.
SELECT t.name, u.username
FROM appname_task t
JOIN appname_assigneduser au
ON (t.id = au.work_id AND au.completed = false)
JOIN appname_employee ae
ON (au.employee_id = ae.id)
JOIN auth_user u
ON (ae.user_id = u.id)
Upvotes: 2
Views: 346
Reputation: 476557
You can obtain this with:
from django.db.models import F
Task.objects.filter(assigneduser__completed=False).values(
'name', username=F('employees__user__username')
)
Upvotes: 3