Re: A better way of checking if a record exists

2010-10-02 Thread ShawnMilo
Bruno, Great point. I've simplified my old code quite a bit with this suggestion. After creating a dictionary with 12 items in it, I use it for the default in get_or_create, then: [setattr(product, field, value) for field, value in new_values.items()] product.save() Thanks, Shaw

Re: A better way of checking if a record exists

2010-10-01 Thread bruno desthuilliers
On 1 oct, 12:34, ALJ wrote: > ... what is the infamous obj.__dict__.update() hack? http://docs.python.org/library/stdtypes.html#mapping-types-dict http://docs.python.org/reference/datamodel.html http://www.google.fr/search?q=obj.__dict__.update() -- You received this message because you are

Re: A better way of checking if a record exists

2010-10-01 Thread Thomas Guettler
Hi, I really would love the see this dictionary like access: obj=MyModel.objects.get(primary_key) if obj is None: ... does not exist. But the ticket was closed: http://code.djangoproject.com/ticket/5741 Thomas ALJ wrote: > I'm running through a spreadsheet to update customer info. It ta

Re: A better way of checking if a record exists

2010-10-01 Thread ALJ
... what is the infamous obj.__dict__.update() hack? On Oct 1, 11:57 am, bruno desthuilliers wrote: > On 1 oct, 10:29, Shawn Milochik 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 = { > >

Re: A better way of checking if a record exists

2010-10-01 Thread bruno desthuilliers
On 1 oct, 10:29, Shawn Milochik 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

Re: A better way of checking if a record exists

2010-10-01 Thread Shawn Milochik
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.obje

A better way of checking if a record exists

2010-10-01 Thread ALJ
I'm running through a spreadsheet to update customer info. It takes quite a while and I was just trying to optimize things a bit. I want to check if the record is already there and if so update it with the newer data. If not append it to the table. Two code snippets below ... try: customer =