Answer the question
In order to leave comments, you need to log in
How to filter by form like this in django orm?
There is a model with fields
class Order(models.Model):
o1 = models.BooleanField()
o2 = models.BooleanField()
o3 = models.BooleanField()
o4 = models.BooleanField()
o5 = models.BooleanField()
# и т.д.
class OrderForm(form.ModelForm):
class Meta:
model = Order
Order.objects.filter(o1=form.cleaned_data['o1'], o2=form.cleaned_data['o2'])
# но тогда получим, что в каком то может быть o1=True, но o2=False и орм ничего не найдет.
Order.objects.filter(Q(o1=form.cleaned_data['o1']) | Q(o2=form.cleaned_data['o2']))
#получим что при o1=False мы не найдем
пока в голову ничего не пришло, мож кто подскажет?
Answer the question
In order to leave comments, you need to log in
Roughly something like this:
myquery &= Q(o1__exact=form.cleaned_data['o1'])
myquery &= Q(o2__exact=form.cleaned_data['o2'])
myquery &= Q(o3__exact=form.cleaned_data['o3'])
table = Order.objects.filter(myquery)
Apparently you need to check for the presence of a positive value in the field.
Here's an option for you:
Order.objects.filter(**{k: v for k, v in form.cleaned_data.items() if k.startswith('o') and int(v)})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question