Django excluding specific instances from queryset without using field lookup
Posted
by Agos
on Stack Overflow
See other posts from Stack Overflow
or by Agos
Published on 2010-06-13T11:46:50Z
Indexed on
2010/06/13
11:52 UTC
Read the original article
Hit count: 353
Hi,
I sometimes have the need to make sure some instances are excluded from a queryset.
This is the way I do it usually:
unwanted_instance = Mymodel.objects.get(pk=bad_luck_number)
uninteresting_stuff_happens()
my_results = MyModel.objects.exclude(id=unwanted_instance.id)
or, if I have more of them:
my_results = MyModel.objects.exclude(id_in=[uw_in1.id, uw_in2.id, uw_in3.id])
This 'feels' a bit clunky, so I tried if I was lucky:
my_ideally_obtained_results = MyModel.objects.exclude(unwanted_instance)
Which doesn't work. But I read here on SO that a subquery can be used as parameter for exclude.
Am I out of luck? Am I missing some functionality (checked the docs, but didn't find any useful pointer)
© Stack Overflow or respective owner