0

Is there an easy way to update a field on a get of a get_or_create? I have a class ItemCategory and I want to either create a new entry or get the already created entry and update a field (update_date). What I do is:

item,created= ItemCategory.get_or_create(cat=cat_id,item=item_id)
if created == True:
     print "created"
else:
     item.update_date = datetime.now
     item.somethingelse = 'aaa'
     item.save()

This works for a while in my loop. But after 50-100 get/create it crashes:

peewee.InterfaceError: Error binding parameter 4 - probably unsupported type.

Maybe I should use upsert(), I tried but wasn't able to get anything working. Also it's not probably the best solution, since it makes a replace of the whole row instead of just a field.

I like peewee, it's very easy and fast to use, but I can't find many full examples and that's a pity

maugch
  • 1,276
  • 3
  • 22
  • 46

1 Answers1

2

Newbie mistake

item.update_date = datetime.now()

I am not 100% sure this is the only answer though. I modified my code so many times that it might be also something else.

Regarding my question about create_or_update , I've done this:

try:
    Item.create(...)
except IntegrityError:
    Item.update(...)

peewee is really great, I wonder why no one ever asked for a create_or_update.

maugch
  • 1,276
  • 3
  • 22
  • 46