1

Consider my model (MODEL) has field has attribute NAME.

I want to reference to that model attribute using string.

eg. var = Model.objects.get( pk=1 )

var.str('NAME') = value

while trying with string it's giving me error : can't assign to function call

P.S.: I know for finding a value of attribute from string I can use getattr() but since I want to assign value, this method isn't helpful.

alter123
  • 601
  • 2
  • 11
  • 32

1 Answers1

5

You are looking for the setattr function.

setattr(x, 'foobar', 123)

More details

Oleksandr Dashkov
  • 2,249
  • 1
  • 15
  • 29