search

Home  >  Q&A  >  body text

python - How does Django implement the following dynamic query with empty parameters?

Enter any one of the last six digits of your mobile phone number or ID card, and one of the results will be returned.
Enter the mobile phone number and the last six digits of the ID card at the same time, and the result will be returned.

I would like to ask how to deal with this logic?

PHPzPHPz2801 days ago954

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-27 17:41:19

    mobile = request.GET.get('mobile', None)
    id_no = request.GET.get('id_no', None)
    
    data_list = XXX.objects.all()
    if mobile:
        data_list = data_list.filter(mobile=mobile)
    if id_no:
        data_list = data_list.filter(id_no=id_no)

    reply
    0
  • 迷茫

    迷茫2017-05-27 17:41:19

    filter = {}
    if mobile:
        filter['mobile'] = mobile
    if card:
        filter['card'] = card
    if status:
        filter['status'] = status
    
    TableModel.objects.filter(**filter)
    

    reply
    0
  • Cancelreply