On Fri, Feb 26, 2010 at 12:11 PM, Phlip <phlip2...@gmail.com> wrote:
> Djangoids:
>
> Consider this pattern:
>
>    product.brand = brand
>    product.in_stock = True
>    product.save()
>
> Because business-layer code does that all the time, can we DRY the
> code up a little?
>
> def up(model, **kw):
>    for key, value in kw.iteritems():
>        model.__dict__[key] = value
>
>    model.save()
>
> That provides this:
>
>   up(product, brand=brand, in_stock=True)
>
> It indeed looks kewt, but it does not always work. If, for
> example, .brand is a model object, then it ain't in the __dict__.
>
> So is there some more clever way to do this?

setattr(model, key, value)

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

Reply via email to