I have a django operation that I'm performing in 2 steps, but I'd like to see if there's a way to do it with just 1 database query. Say I have these models:
class Budget(models.Model):
...
class Company(models.Model):
budgets = models.ManyToManyField(
Budget, related_name="companies"
)
class Employee(models.Model):
company = models.ForeignKey(
Company, on_delete=models.CASCADE, related_name="employees"
)
Now I want to get an employee, and find the IDs of the budgets that are associated with this employee, through the company:
employee = Employee.objects.get(id=employee_id)
allowed_budgets = employee.company.budgets.values_list("id", flat=True)
Am I correct that this would perform 2 database queries? Is there a way to add allowed_budgets as an annotation on Employee so only 1 query is performed?
employee = Employee.objects
.annotate(
allowed_budgets=# how do I get employee.company.budgets.values_list("id", flat=True) here?
)
.get(id=employee_id)
Thanks a lot!