django object get/set field

why do you want this?

You could use

obj.__dict__['field']

i guess... though it's not a method call

changed=[field for (field,value) in newObj.__dict__ if oldObj.__dict__[field] != value]

will give you a list of all the fields that where changed.

(though I'm not 100% sure)


If somebody stumbles upon this little question, the answer is right here: How to introspect django model fields?


To get the value of a field:

getattr(obj, 'field_name')

To set the value of a field:

setattr(obj, 'field_name', 'field value')

To get all the fields and values for a Django object:

[(field.name, getattr(obj,field.name)) for field in obj._meta.fields]

You can read the documentation of Model _meta API which is really useful.