Problems building a dictionary to return a Queryset

1

When I want to obtain a queryset filtering according to several parameters I use a dictionary, for example:

queryset = Actividad.objects.filter(**my_filter)

Where my_filter has been constructed in this way:

if nombre:
        my_filter["nombre"] = nombre

    if es_principal:
        my_filter["es_principal"] = es_principal

    if fecha_inicio:        
        my_filter["fecha_inicio"] = fecha_inicio

nombre , es_principal , fecha_inicio are fields of a model Activity and come in the data of a request.

So far I have filtered when I receive values exactly like the ones I have stored in database, for example look for the name "Reunion" and in my database there is an activity with the same name.

What I want to know is how to build the dictionary to filter when the name contains one or several words, or when the date is greater than the date sent in the request. Try to do the following but it did not work:

my_filter["nombre"] = Q(nombre__like = datos["nombre"])

my_filter["fecha_inicio"] = Q(fecha_inicio__gte = datos["fecha_inicio"])
    
asked by Ethan 15.02.2018 в 15:10
source

1 answer

2

The lookups in Django are parsed internally so by using them as Dictionary keys should work:

my_filter["nombre__contains"] = datos["nombre"]

my_filter["fecha_inicio__gte"] = datos["fecha_inicio"]

queryset = Actividad.objects.filter(**my_filter)

By the way, there is no lookup __like , for that there is the __contains or the __icontains .

    
answered by 15.02.2018 / 20:03
source