[Django]-Django query filter with variable column

208👍

Almost there..

members.filter(**{'string__contains': 'search_string'})

To understand what it’s doing, google around : )
Understanding kwargs in Python

** expands dictionary key/value pairs to keyword argument – value pairs.

To adapt your example to the solution:

variable_column = 'name'
search_type = 'contains'
filter = variable_column + '__' + search_type
info=members.filter(**{ filter: search_string })

-3👍

Syntax:

model_name.objects.filter(column_name='value')

Ex: In my scenario, I wanted to find out all records with status completed from the Student table.

Student.objects.filter(status="completed")

Leave a comment