Reputation: 35
I'm trying to make a custom query in django based on some filters located in the template. Should be something similar with the View tickets section from Django official website
My template should have some inputs for predefined columns in the Filter section and based on the given values should filter the results.
I found some topics related to Q objects, but I'm not sure how to build the queries based on the values from UI.
Do you have any ideas?
Upvotes: 2
Views: 1021
Reputation: 10850
We've implemented a query builder, akin to what you're suggesting, for the django admin over at django-advanced-filters.
You can probably copy paste the form/s and related templates for a similar effect outside of the admin.
Upvotes: 0
Reputation: 1511
Q objects is use for SQL OR and AND e.g
Select * From Country Where Country_Name LIKE "India" OR Country_Name LIKE "Pakistan"
Q(question__startswith='Who') | Q(question__startswith='What')
This is equivalent to the following SQL WHERE clause:
WHERE question LIKE 'Who%' OR question LIKE 'What%'
https://docs.djangoproject.com/en/1.9/topics/db/queries/#complex-lookups-with-q-objects
Upvotes: 1