I am having difficulty updating a model object in a view when the
field to be updated is determined by the data submitted in the
request.  For instance, if the model has some arbitrary fields, and
the request submitted includes POST data indicating that one of those
fields should be updated, what is the correct syntax?

For example:

object = ModelName.objects.get(id=request.POST.get("pk"))
fieldName = request.POST.get("fieldName")
value = request.POST.get("value")

>> All of the above assignments have been verified to have worked as intended.

object.fieldName = value
>> doesn't work... I get an error indicating that ModelName has no attribute 
>> 'fieldName'.  I never expected this to work.  In Perl, I would use a syntax 
>> like object.{fieldName}.  In Python, I expected something like the next 
>> example to work.

object[fieldName] = value
>> doesn't work... I get an error indicating that ModelName object does not 
>> support item assignment.

eval('object.' + fieldName + ' = "' + value + '"')
>> doesn't work... I get a syntax error.

Any suggestions would be greatly appreciated.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to