Friday, July 9, 2010

how to dynamically update model fields?

I want to provide a method as follows,

def update_some_model_field(key, field_name, value):
'''user provides the primary key of row, field name in model and the
value and the method updates the field for that row with the value'''

1. model_obj = Some_model.objects.get(pk = key)
2. model_obj."field_name" = value ## HOW to make this line generic??
I do not want to do lots of If as shown below.
3. model_obj.save()


For line 2 another way to write it is as follows, which I want to
avoid:

if field_name == "field_a":
model_obj.field_a = value
elif field_name == "field_b":
model_obj.field_b = value
and so on... for 20 fields of 20 models....

Any suggestions?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate