Answer the question
In order to leave comments, you need to log in
How to search for object instances by empty fields?
I have a form that has several optional filters: age, eye color, zodiac sign, and so on. If the user did not specify all this, there will be no filtering by the specified parameter.
And here is the problem: these fields are optional not only for the search form, but also for the registration form, that is, questionnaires with empty values are possible.
To filter by age, I did this (I know that it is necessary to classify, but I will rewrite later):
def new_services_search(request, name='', ages=['18_24', '24_35', '36_42', '43+', None]):
if request.method=='POST':
data = request.POST
name = data.get('name')
if data.get('age'):
age = list(data.getlist('age'))
filter_girls = Profile.objects.filter(available=True, name__istartswith=name,age__in=ages)
Answer the question
In order to leave comments, you need to log in
The code is a rough sketch and needs some tweaking, but what you want is something like, as far as I understand it:
from django.db.models import Q
# Это лучше хранить где-нибудь в базе, а не в коде
DEFAULTS = {
'name': '',
'age': ['18_24', '24_35', '36_42', '43+', None],
'eye': None,
}
def new_services_search(request):
if request.method=='POST':
filters = []
for k in request.POST:
kwargs = {}
v = request.POST.get(k, DEFAULTS[k])
if v is None:
kwargs[k + '__isnull'] = True
elif isinstance(v, list):
kwargs[k + '__in'] = v
else:
kwargs[k] = v
filters.append(Q(**kwargs))
filter_girls = Profile.objects.filter(filters)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question