CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/html;charset=utf-8
cache-control: must-revalidate
expires: Fri, 01 Jan 1999 00:00:00 GMT
set-cookie: trac_form_token=378ca9daedacff11dc10cc3f; HttpOnly; Path=/; Secure
set-cookie: trac_session=36c7a9fd6c041c413c8bfca0; expires=Tue, 21 Oct 2025 22:39:56 GMT; HttpOnly; Path=/; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
permissions-policy: interest-cohort=()
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
accept-ranges: bytes
via: 1.1 varnish, 1.1 varnish
date: Wed, 23 Jul 2025 22:39:56 GMT
x-served-by: cache-fra-etou8220048-FRA, cache-bom-vanm7210072-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753310396.571815,VS0,VE829
vary: Accept-Encoding
OrQueryBuilder – Django
Back to Top
Django
The web framework for perfectionists with deadlines.
Issues
Sometimes one may wish to query the database for objects matching any of several criteria specified in several form fields, some of which may be multiple choice. This function makes that easy:
def buildOrQuery(request, config): ''' Builds a django.db.models.query.QOr object based on HTTP request and a simple config dictionary, mapping field names to filter syntax. For example: config = { 'bar': 'foo__bar__exact', 'ham': 'spam__eggs__ham__icontains', } ''' query = None # A blank query here would be v bad for field in config.keys(): values = request.POST.getlist(field) for value in values: subquery = Q(**{config[field]: value}) if not query: query = subquery else: # Combine using OR: query = query | subquery if query: return query else: return Q() # Blank query -- it would break things to return None
Last modified
18 years ago
Last modified on Aug 14, 2007, 4:03:49 PM
Note:
See TracWiki
for help on using the wiki.
Download in other formats:
Django Links
Learn More
Get Involved
Follow Us
- Hosting by In-kind donors
- Design by Threespot &
© 2005-2025 Django SoftwareFoundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.