For my Django app I have Events, Ratings, and Users. Ratings are related to Events and Users through a foreign keys. When displaying a list of Events I want to filter the ratings of the Event by a user_id so I know if an event has been rated by the user.
If I do:
event_list = Event.objects.filter(rating__user=request.user.id)
(request.user.id gives the user_id of the current logged in user) ...then I only get the events that are rated by the user and not the entire list of events.
What I need can be generated through the custom SQL:
SELECT *
FROM `events_event`
LEFT OUTER JOIN (
SELECT *
FROM `events_rating`
WHERE user_id = ##
) AS temp
ON events_event.id = temp.user_id
Is there an easier way so I don't have to use custom SQL?