This works excellent. Had now idea this was possible... Regards, Stefan Tim Chase escribió: I have a qset object like this: qset = ( Q(dst__contains=company.phone) | Q(dst__contains=company.cellphone) | Q(dst__contains=company.fax) | Q(src__contains=company.phone) | Q(src__contains=company.cellphone) | Q(src__contains=company.fax) )If a company has a None value in one of these fields, I get a "Cannot use None as a query value" error. I would want to add Q objects dynamically depending on the situation. Is this possible?Just do it in a loop (knowing about dict expansion with the "**" helps): items = ( ("dst", company.phone), ("dst", company.cellphone), ("dst", company.fax), ("src", company.phone), ("src", company.cellphone), ("src", company.fax), ) qset = Q() for name, value in items: if value is None: continue qset |= Q(**{"%s__contains" % name : value}) This might even be reducible to qset = Q() for field1 in ("phone", "cellphone", "fax"): value = getattr(company, field1) if value is None: continue for field2 in ("src", "dst"): qset |= Q(**{"%s__contains" % field2: value}) You might also want to check that the results have at least *one* value (that at least one of ph/cell/fax was non-None) -tim --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~--- |
- Dynamically changing q object Stefan Tunsch
- Re: Dynamically changing q object Sebastian Bauer
- Re: Dynamically changing q object Sebastian Bauer
- Re: Dynamically changing q object Tim Chase
- Re: Dynamically changing q object Stefan Tunsch