Make a Django query filter at runtime

56 views
Skip to first unread message

Ezequias Rocha

unread,
Jan 8, 2020, 8:08:41 PM1/8/20
to Django users
Hi everyone

I am in a doubt about creating django filters dynamically.

All you know a filter is made by using the parameters like:

model.filter(name='John', age=42)

But if I can't type all fields and values at design time but at runtime how to do that?

Best regards
Ezequias

Shaheed Haque

unread,
Jan 8, 2020, 11:42:55 PM1/8/20
to django...@googlegroups.com
Populate a dictionary with what you need and pass that in:

query = {'name': 'John', 'age': 42}
model.filter(**query)

 

Best regards
Ezequias

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9c843d02-235f-411f-8e83-7fea2156893b%40googlegroups.com.

Alex Conselvan de Oliveira

unread,
Jan 9, 2020, 1:08:18 AM1/9/20
to django...@googlegroups.com
Hi Ezequias,

You could use a dict:

data = {
  'name': 'John',
  'age': 42,
}

model.filter(**data)

Best Regards!

--

Ezequias Rocha

unread,
Jan 9, 2020, 12:32:29 PM1/9/20
to Django users
Thank you very much Alex and Shaheed, but if I want to use the >, >= <, <= how do do that?


On Wednesday, January 8, 2020 at 10:08:18 PM UTC-3, Alex Conselvan de Oliveira wrote:
Hi Ezequias,

You could use a dict:

data = {
  'name': 'John',
  'age': 42,
}

model.filter(**data)

Best Regards!

Em qua., 8 de jan. de 2020 às 18:09, Ezequias Rocha <ezequi...@gmail.com> escreveu:
Hi everyone

I am in a doubt about creating django filters dynamically.

All you know a filter is made by using the parameters like:

model.filter(name='John', age=42)

But if I can't type all fields and values at design time but at runtime how to do that?

Best regards
Ezequias

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Ezequias Rocha

unread,
Jan 9, 2020, 4:50:13 PM1/9/20
to Django users
It worked well. Many thanks.


On Wednesday, January 8, 2020 at 10:08:18 PM UTC-3, Alex Conselvan de Oliveira wrote:
Hi Ezequias,

You could use a dict:

data = {
  'name': 'John',
  'age': 42,
}

model.filter(**data)

Best Regards!

Em qua., 8 de jan. de 2020 às 18:09, Ezequias Rocha <ezequi...@gmail.com> escreveu:
Hi everyone

I am in a doubt about creating django filters dynamically.

All you know a filter is made by using the parameters like:

model.filter(name='John', age=42)

But if I can't type all fields and values at design time but at runtime how to do that?

Best regards
Ezequias

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Ezequias Rocha

unread,
Jan 10, 2020, 12:35:48 PM1/10/20
to Django users
Hi Alex

I could do almost all query but not the "not equals" could you give me some tip to add this operator to my object?

I am doing the following way:

  if (operador == 'equals'):
            d[realname] = value
  elif (operador == greather than'):
            d[realname + '__gt'= value
        elif (operador == 'less than'):
            d[realname + '__lt'= value
elif (operador == 'not equals'):
            ????
. . .
qs = classDjango.objects.filter(**d)

    return qf


Best regards
Ezequias

On Wednesday, January 8, 2020 at 10:08:18 PM UTC-3, Alex Conselvan de Oliveira wrote:
Hi Ezequias,

You could use a dict:

data = {
  'name': 'John',
  'age': 42,
}

model.filter(**data)

Best Regards!

Em qua., 8 de jan. de 2020 às 18:09, Ezequias Rocha <ezequi...@gmail.com> escreveu:
Hi everyone

I am in a doubt about creating django filters dynamically.

All you know a filter is made by using the parameters like:

model.filter(name='John', age=42)

But if I can't type all fields and values at design time but at runtime how to do that?

Best regards
Ezequias

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Derek

unread,
Jan 11, 2020, 8:16:26 AM1/11/20
to Django users
There is a long discussion about "not equals" here:


I am sure you can adapt one of the suggestions there for your specific needs.
Reply all
Reply to author
Forward
0 new messages