Saturday, July 10, 2010

Re: how to dynamically update model fields?

On Sat, Jul 10, 2010 at 12:01 AM, zweb <traderashish@gmail.com> wrote:
> 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()
[...]
>
> Any suggestions?

setattr(model_obj, field_name, value)

~Rolando

--
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