I have a model with a simple ratio
class Tasks(models.Model): initiator = models.ForeignKey(User, on_delete = models.CASCADE) class TaskResponsiblePeople(models.Model): task = models.ForeignKey('Tasks') auth_user = models.ForeignKey(User)
And I need to write an analogue of the SQL query as follows:
select a.initiator, b.auth_user from Tasks a inner join TaskResponsiblePeople b on TaskResponsiblePeople.task_id = task.id where Tasks.initiator = 'value A' OR TaskResponsiblePeople.auth_user = 'value B'
The problem is that the OR statement deals with two different tables, and I don't know how to use the Django syntax correctly to reproduce the above raw-SQL query. Help me please!
UPDATE 1
According to the answer below, I am using the following code:
people = TaskResponsiblePeople.objects.filter(Q(task__initiator = request.user.id)|Q(auth_user = request.user.id)).select_related('auth_user') print people.query # The result of the print copy-pasted from console # SELECT * FROM `task_responsible_people` # LEFT OUTER JOIN `tasks` ON (`task_responsible_people`.`task_id` = `tasks`.`id`) # LEFT OUTER JOIN `auth_user` T4 # ON (`task_responsible_people`.`auth_user_id` = T4.`id`) # WHERE (`tasks`.`initiator_id` = 7 OR # 'task_responsible_people`.`auth_user_id` = 7) tasks = Tasks.objects.prefetch_related( Prefetch('task_responsible_people', queryset=people, to_attr='people'))
However, in the final result set, I still see entries where neither the initiator nor auth_user are equal to request.user (equal to 7 in this case) I avoid using ".values" due to the potential need to serialize and convert the request to json.
source share