sql - Django filter multiple columns with a list of tuples -
i have simple model 2 fields:
class simple(model) class meta: index_together = true = intfield() b = intfield()
i generate sql query tuples of values a,b. e.g.
select * simplemodel (a,b) in ((1,1), (4,8), ...)
i know how create like:
select * simplemodel ((a = 1 , b = 1) or (a = 4 , b = 8))
which logically same, think db has problem when number of possible values big (i using postgresql), query longer it's heavier on network, , harder analyze , read correctly (i.e. use composite index in case).
so, question is, can django create query in first form?
thanks!
yes, only* using where
parameter of extra
: https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.queryset.extra
something should do:
simple.objects.extra(where=['(a,b) in %s' % your_list])
*if create custom database type should able define custom operators, so... might able work around it. i'll google bit :)
Comments
Post a Comment