0

I have One String in python

field_name = "status"
value = "joined"

I want to convert this string to some variable name like,

status = "joined"

my Indention is Django ORM

model.objects.filter(status = 'joined')

Any Suggestion doing for this..

Here am reffered this URL Convert string to variable name in python

but here it happend only integer. not working string type of values any one help me

2 Answers2

2

Just for Django ORM you can pass parameters as a dictionary:

field_name = "status"
value = "joined"

filter_params = {
    field_name: value
}

model.objects.filter(**filter_params)

or just:

field_name = "status"
value = "joined"

model.objects.filter(**{field_name: value})

See this page for more details on unpacking a dictionary.

Don
  • 16,928
  • 12
  • 63
  • 101
0

Try this

field_name = "status" 
value = "joined"

exec(f'{field_name} = "{value}"')
Reza
  • 1,945
  • 1
  • 9
  • 17