Django检查查询中是否有任何attr匹配

时间:2019-01-31 13:37:44

标签: python html django

我有一个“搜索”表单,用于搜索数据库中的对象。 该表单有两个字段,一个Select(选择字段),用户可以在其中选择要搜索的属性,一个文本字段可以填写。

例如,我可以选择“按名称搜索”,然后键入“ Matt”,这样它将在“名称”列上搜索值“ Matt”。 我也可以按其他属性进行搜索。

一切正常,直到这里。当我尝试使用我的最后一个搜索选项“按任意巧合搜索”时,就会出现问题,该选项应在任何列中查询匹配项。

forms.py:

class BuscarAuxi(forms.Form):
    busqPor = forms.ChoiceField(
        required=False,
        widget=forms.Select,
        choices=SEARCH_AUXI,
    )
    criterio = forms.CharField(max_length=100)
    busqPor.widget.attrs.update({'class': 'form-control'})
    criterio.widget.attrs.update({'class': 'form-control col-7 m-2', 'placeholder': 'Criterio'})

views.py:

def crudAuxi(request):
if request.method == "GET":
form = BuscarAuxi(request.GET)
print('Get method. ')
print(form.errors)
if form.is_valid():
    print(Valid form. ')
    print(form.cleaned_data.get('busqPor'))
    critBusqueda = form.cleaned_data.get('criterio')
    tipoBusqueda = form.cleaned_data.get('busqPor')
    if tipoBusqueda == '1':
        try:
            busqueda = Auxi.objects.get(denom=critBusqueda)
            if busqueda:
                print('Matched by name. ')
        except ObjectDoesNotExist:
            print('Not found by name. ')
    if tipoBusqueda == '2':
        try:
            busqueda = Auxi.objects.get(nroDoc=critBusqueda)
            if busqueda:
                print('Matched by DNI. ')
        except ObjectDoesNotExist:
            print('Not found by DNI. ')
    if tipoBusqueda == '3':
        try:
            busqueda = Auxi.objects.get(cuit=critBusqueda)
            if busqueda:
                print('Matched by CUIT. ')
        except ObjectDoesNotExist:
            print('Not found by CUIT. ')
    if tipoBusqueda == '4':
        try:
            busqueda = Auxi.objects.get(tele=critBusqueda)
            if busqueda:
                print('Matched by phone number. ')
        except ObjectDoesNotExist:
            print('Not found by phone number. ')
    if tipoBusqueda == '5':
        try:
            busqueda = Auxi.objects.get(critBusqueda)
            if busqueda:
                print('Found by any attr. ')
        except ObjectDoesNotExist:
            print('Not found by any attr. ')

从1到4的选项正常工作,但是选项5不起作用(我知道此选项的语法错误),我试图弄清楚如何在任何列/属性中搜索匹配项

1 个答案:

答案 0 :(得分:1)

您可以使用filter()来避免使用try except,如果要检查它是否仅存在,可以使用exists()来返回布尔值。

from django.db.models import Q

if form.is_valid():
    print('Valid form. ')
    print(form.cleaned_data.get('busqPor'))
    critBusqueda = form.cleaned_data.get('criterio')
    tipoBusqueda = form.cleaned_data.get('busqPor')
    if tipoBusqueda == '1':
        busqueda = Auxi.objects.filter(denom=critBusqueda).exists()
        if busqueda:
            print('Matched by name. ')
        else:
            print('Not found by name. ')
    if tipoBusqueda == '2':
        busqueda = Auxi.objects.filter(nroDoc=critBusqueda).exists()
        if busqueda:
            print('Matched by DNI. ')
        else:
            print('Not found by DNI. ')
    if tipoBusqueda == '3':
        busqueda = Auxi.objects.filter(cuit=critBusqueda).exists()
        if busqueda:
            print('Matched by CUIT. ')
        else:
            print('Not found by CUIT. ')
    if tipoBusqueda == '4':
        busqueda = Auxi.objects.filter(tele=critBusqueda).exists()
        if busqueda:
            print('Matched by phone number. ')
        else:
            print('Not found by phone number. ')
    if tipoBusqueda == '5':
        busqueda = Auxi.objects.filter(
            Q(column1=critBusqueda) |
            Q(column2=critBusqueda) |
            Q(column3=critBusqueda) |
        ).exists()
        if busqueda:
            print('Found by any attr. ')
        else:
            print('Not found by any attr. ')

5中,您检查column1column2column3是否与您的查询匹配。其中column1column2column3是您的字段。