4👍
Your question is really unclear about what you actually expect, but here are a couple hints anyway:
First, model instances (assuming they are instances of the same model of course) compare on their primary key value, which is also used as hash for dicts and sets, so if you want to compare the underlying database records you should not work on model instances but on the raw db values as lists of tuples or dicts. You can get those using (resp.) Queryset.values_list()
or Queryset.values()
– not forgetting to list()
them so you really get a list
and not a queryset
.
Which brings us to the second important point: while presenting themselves as list-likes (in that they support len()
, iteration, subscripting and – with some restrictions – slicing), Querysets
are NOT lists
. You can’t compare two querysets (well you can but they compare on identity, which means that two querysets will only be equal if they are actually the very same object), and, more important, using a queryset as an argument to a ‘field__in=’ lookup will result in a SQL subquery where passing a proper list results in a plain ‘field IN (…)’ where clause. This explains the error you get with the exclude(...)
approach.
To make a long story short, if you want to effectively compare database rows, you want:
# the fields you want to compate records on
fields = 'field1', 'field2', 'fieldN'
rows1 = list(YouModel.objects.using('tmp1').filter(...).values_list(*fields))
rows2 = list(YouModel.objects.using('tmp2').filter(...).values_list(*fields))
# now you have two lists of tuples so you can apply ordinary python comparisons / set operations etc
print rows1 == rows2
print set(rows1) - set(rows2)
# etc