[Django]-Django model field by variable

85👍

You can use pythons getattr function to do this. Pass the field name in as the attribute.

getattr(model, fieldtoget)

Since fieldtoget is a variable, this is dynamic.

You can use setattr to set it the same way.

Leave a comment