I build a list of Django model objects by making several queries. Then I want to remove any duplicates, (all of these objects are of the same type with an auto_increment int PK), but I can’t use set() because they aren’t hashable.
Is there a quick and easy way to do this? I’m considering using a dict instead of a list with the id as the key.
That’s exactly what I would do if you were locked into your current structure of making several queries. Then a simply
dictionary.values()will return your list back.If you have a little more flexibility, why not use
Qobjects? Instead of actually making the queries, store each query in aQobject and use a bitwise or (‘|’) to execute a single query. This will achieve your goal and save database hits.Django Q objects