Referencing Model Instance Fields Dynamically

Created on Oct. 27, 2012, 10:23 a.m. by Hevok & updated by Hevok on May 2, 2013, 5:26 p.m.

To update an prior undeclared model instance using a fields dictionary settattribute can be used::

for k,v in fields.iteritmes():
   setattr(instance, k, v

In the case of ManyToMany relationship attributes its counterpart gettattribute comes in handy::

getattr(instance, k).add(relatedObject)

Tags: django
Categories: Tutorial
Parent: Web Framework

Update entry (Admin) | See changes

Comment on This Data Unit