Reputation: 6031
I have two QuerySets in Django:
a = [<Character: Character object>, <Character: Character object>, <Character: Character object>]
b = [<Entity: Entity object>, <Entity: Entity object>, <Entity: Entity object>, <Entity: Entity object>, <Entity: Entity object>, <Entity: Entity object>]
I'd like to avoid multiple for
loops to check if Character.someattr
is identical with Entity.someattr
and return a list with Character
objects, so what would be the easiest way to do that?
Upvotes: 2
Views: 3575
Reputation: 3294
I think the easiest way is to filter queryset. This way may contain additional query, but it must be much faster, especially with large data.
attrs_list = Entity.objects.filter(**filters).distinct().values_list('someattr', flat=True)
a = Character.objects.filter(someotherattr__in=attrs_list)
Upvotes: 6