9👍
✅
You may want to look into Q
objects. Briefly, the Q
lets you use OR
statements in your queries. From the aforelinked page:
Polls.objects.filter(
Q(question__startswith='Who') | Q(question__startswith='What')
)
This will pull questions starting with "Who" OR "What".
23👍
What you actually can do
from django.db.models import Q
q = Q()
if param_1 != 'all':
q &= Q(param_1__name=param_1)
if param_2 != 'all':
q &= Q(param_2__name=param_2)
class_var = ClassName.objects.filter(q)
OR
q = {}
if param_1 != 'all':
q.update({'param_1__name': param_1})
if param_2 != 'all':
q.update({'param_2__name': param_2})
class_var = ClassName.objects.filter(**q)
- How to find out whether a model's column is a foreign key?
- Using SQLite3 with Django 2.2 and Python 3.6.7 on Centos7
- Django – Change a ForeignKey relation to OneToOne
3👍
Use Q objects an make an list of queries.
q=[]
if param_1 != 'all':
q.append(Q(param_1__name=param_1))
if param_2 != 'all':
q.append(Q(param_2__name=param_2))
details = ClassName.objects.filter(reduce(AND, q))
- Django/Graphene/Apollo/django-webpack-loader/Vue: CORS/CSRF not working together?
- GeoDjango: How to create a circle based on point and radius
1👍
Querysets are lazy. There won’t be a database query until the queryset is evaluated, usually by iterating. You’re free to apply as many filters as you like at virtually no cost.
One note: filter()
does not change the queryset, but rather returns a new queryset. The correct way to apply additional filters would be:
class_var = class_var.filter(param_1__name=param_1)
👤knbk
- Relation does not exist error in Django
- Django S3 – trim imagefield filename but not the url path
- How do I restart gunicorn hup , i dont know masterpid or location of PID file
- Django – How to send a success message using a UpdateView CBV
- What could cause a Django error when debug=False that isn't there when debug=True
Source:stackexchange.com