How to apply a filter in django queryset only if a condition is met. I have a filter object that contains forms list. if forms contains "all" then i want to fetch all objects of the AnswerDetails model else i want to fetch only the u_id in the forms list
Code:
fil = self.d.get('filter', None)
f_uid = fil.get('forms',["all"])
if "all" in f_uid:
f_uid = []
a = AnswerDetails.objects.filter(proj=_p,
form__u_id__in=f_uid).order_by('-saved_on')
You can just remove form filtration:
a = AnswerDetails.objects.filter(proj=_p).order_by('-saved_on')
If you want to exclude something then you can also do this
a = AnswerDetails.objects.filter(proj=_p).exclude(form__u_id__in=f_uid).order_by('-saved_on')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.