2

I want to update a table field by filtering the data and selecting a specific row, in below query "address1" is the field of User table which needs to be updated with value available in variable addr:

User.objects.filter(user_id=uid).update(address1=addr)

Above query works fine as I have given column name ="address1" in update option.

But my requirement is I want update field to be dynamic, i.e. it can be address1,address2, address3 (this is stored in a variable) Let's say it is stored in add_field variable then I can't use below as it will try to find add_field column in User table which is not present there:

User.objects.filter(user_id=uid).update(add_field=addr)

Query : How can I pass field name through variable/dynamically to update option in mentioned line of code or through any other option.

1
  • 1
    How you tried unpacking a dictionary with variables?User.objects.filter(user_id=uid).update(**{add_field:addr}) Commented Feb 2, 2022 at 10:34

1 Answer 1

1

You can unpack a dictionary full of your keyword arguments, like so:

User.objects.filter(user_id=uid).update(**{add_field:addr})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.