Generate querysets from dynamically generated rules

0

I'd like to do a sort of filter using Django. Imagine the following case:

  • Model: user (Name, gender, age, natal_day, birth_ month, etc.)
  • Rule: Catch all users with Gender = M and Age > 20

The intent would be to generate rules from an Application and these rules would be several E and OU chained. If anyone can give me a map or tell me how they solved a similar problem and I would be very grateful.

    
asked by anonymous 14.05.2018 / 20:01

2 answers

2

You can use Q () objects to compose your querysets dynamically (

I wrote in my Medium blog a tutorial illustrating how to compose these dynamic queries, they are even faster and safer (avoid race conditions) than the native Django filter ().

link

    
15.05.2018 / 02:54
3

Take a look at Django Filters. And super powerful, already has standard behaviors that I think will suit you, but you can also create your own filters classes and within them implement their rules link

    
14.05.2018 / 22:51