... what is the infamous obj.__dict__.update() hack?

On Oct 1, 11:57 am, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
> On 1 oct, 10:29, Shawn Milochik <sh...@milochik.com> wrote:
>
>
>
> > For what it's worth, I use the get_or_create with the 'default' keyword to 
> > do this:
>
> > for record in reader:
>
> >     new_values = {
> >         'product_code': slugify(record['Product Code']),
> >         'msrp': record['Suggested Retail'],
> >         #etc...
> >     }
>
> >     product, was_created = Product.objects.get_or_create(source_id = 
> > record['Source ID'], defaults = new_values)
>
> >     if not was_created:
> >         product.product_code = slugify(record['Product Code'])
> >         product.msrp = record['Suggested Retail']
> >         #etc...
>
> Err... What about:
>
>     if not was_created:
>         for attrname, value in new_values.items():
>             setattr(product, attrname, value)
>         product.save()
>
> > I don't like the DRY violation, but a model instance doesn't have an 
> > update() method like a queryset for me to dump the kwargs into -- unless 
> > I'm missing something.
>
> For "direct" model fields you could use the infamous
> obj.__dict.__.update() hack but it's dirty and brittle - and it still
> won't work with foreign keys, m2ms etc.
>
> Now nothing prevents you from providing this model.update method...

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