2011/5/13 Martin Quinta <megustanlaschikasconga...@frikiocio.net>: > i need a serch in db users > (users have city, age,sex.. etc) > > but i need serch users from many citys. > > somthing like this... > from django.db.models import Q > (http://docs.djangoproject.com/en/dev/topics/db/queries/#complex- > lookups-with-q-objects) > users.objects.get(Q(city='xxxx') | Q(city='yyyy'), age_gt = 25, > age_lt= 35, sex='M') > > but in need somthing like this to... > users.objects.get(Q(city=xxx)) > > or meaby this... > user.objects.get( age=24) > > and much more examples.. > > obyusly whit generic method > > i can make a generic method like this: (only work with AND) > dic={age_gt:14,age_lt:23,sex:f} > user.objects.get(**dic) > ##the dictionary get from form wiht checkboxs > > but i cant use this method with OR > > i can make somthing like this... > list_city=[xxx,yyy,zzz....] > dic = {age:23,sex:M.....} > user.objects.get( *[Q(city='x') for x in list_city], **dic ) > but make somthing like this > user....(Q(city=xxx),Q(city=yyyy)) > and need somthing like this > users....(Q(city=xxx) | Q(city=yyyy)) > > any solution??? > or other method?? > meaby put a sql direct? > user.objects.get(explicit_sql='city=1 OR city=2 OR city=3...' , > **other paramethers) >
Use the builtin operator module and the builtin reduce like this: import operator qlist = [] [qlist.append(Q(city=x)) for x in list_city] User.objects.filter(reduce(operator._or, qlist)) Regards, -- Fabián E. Gallina http://www.anue.biz -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.