Filter data /Disregard null value when filtering in Django

青春壹個敷衍的年華 提交于 2021-01-20 13:32:13

问题


Does anybody know how can I filter data even the passing value is False, it so hassle to make lot of conditions I think there is a way to filter data even the value is false or empty.

views.py

def sample(request):
    if request.method=='POST':  
  
      province = request.POST.get('province', False)
      municipality = request.POST.get('municipality', False)
      barangay = request.POST.get('barangay', False)
      status = request.POST.get('stats', False)
      batch= request.POST.get('Pbatch', False)
      it_batch= request.POST.get('Itbatch', False)

      filter= Person.objects.filter(province=province, municipality=municipality, 
      barangay=barangay,status=status,batch=batch,it_batch=it_batch)

      return render .. so on and so on...

The problem is when some data has no value or let say false it didn't recognize the data filter, is there any solution or idea how to achieve this?

来源:https://stackoverflow.com/questions/64893544/filter-data-disregard-null-value-when-filtering-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!