I want to do equivalent query as SELECT user_name, item_name FROM users, items WHERE users.favor_item_id = items.item_id, which is to return user_name and item_name pairs. In the database, one user can have multiple favoured items.
I'm just curious what is a equivalent Django query for this SQL query??
My first thought is to list all (user, favor_item_id) pair from USERS, and then look for item_name with id item_id. But it seems that it will search the ITEM table N times(N is the number of pairs), which has complexity of O(NlogM)(M is number of items in ITEM), while using the SQL query above, the complexity is O(N).
Is there a more efficient way of doing this in django(or any ORM system)?
usersrelation, but in the very next sentence, you say that a user can have more than one favorite item. How is that possible, unless a single user can appear more than once in theuserstable?