I'm familiar with hard-coding filters in the standard fashion, e.g.
posts = Post.objects.filter(title__contains='django').

I'm interested in finding out whether it's possible to replace
title__contains in the above example with a variable. Why do I want to
do this? Well, here's some pseudocode:

posts = Post.objects.all()
if title checkbox is checked:
        posts = posts.filter(title__contains='django')
if subheading checkbox is checked:
        posts = posts.filter(subheading__contains='django')
if body checkbox is checked:
        posts = posts.filter(body__contains='django')

I would love to be able to do this with a loop:

posts = Post.objects.all()
for field in ['title', 'subheading', 'body']:
        posts = posts.filter(field__contains='django')

Clearly the above will not work, but is there a way to achieve the
result I'm after?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.

Reply via email to