Steven Matthews
Steven Matthews

Reputation: 11325

Trying to update Django model with dynamic variable

I am trying to update a Django model with dynamic data, like so:

MyModel.objects.filter(pk=id).update(key=None)

key here is a string variable, based on the key from some array.

However when I try to run this, I get this:

django.core.exceptions.FieldDoesNotExist: MyModel has no field named 'key'

Which makes sense, it is looking for a field key.

How do I tell it I want to use the actual value of the string variable for the key, rather than key?

Upvotes: 1

Views: 1308

Answers (1)

Kryštof Řeháček
Kryštof Řeháček

Reputation: 2483

You can pass dict of keyword arguments to the function calling like so

some_dict = {
    'some_field': 'some value',
}

MyModel.objects.filter(pk=id).update(**some_dict)

Upvotes: 5

Related Questions