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

1
  • Don't do this, it isn't necessary Commented Sep 8, 2020 at 7:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

field_name = "status" 
value = "joined"

exec(f'{field_name} = "{value}"')

3 Comments

exec could be very unsafe. There is a better way by using dict unpacking
locals()[field_name] = value will create the variable without issues
@SardorbekImomaliev agree, but that's one solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.