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"])