Steven Armstrong skrev: > Chris Hoeppner wrote on 08/25/07 18:40: > >> Hi there! >> >> I was just wondering how to dynamically "or" together an indetermined >> quantity of Q objects. They're constructed from a string like q=a+b+c, >> which would get stiched together as "(Q(field=a) | Q(field=b) | >> Q(field=c))". Any clue on how to do it with unknown parameters? It will >> need to work with a+b, just like a+b+c+d+e+f+g+h... You get it :) >> > > from django.db.models import Q > > fields = 'a+b+c+d+e'.split('+') > > query = Q(field=fields.pop(0)) > for field_value in fields: > query = query | Q(field=field_value) > mylist = MyModel.objects.filter(query) > Or, if you prefer a more functional style:
from django.db.models import Q import operator fields = 'a+b+c+d+e'.split('+') query = reduce(operator.or_,(Q(field=field_value) for field_value in fields), Q()) mylist = MyModel.objects.filter(query) (only tested for syntax) Nis --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---