linkyndy/remodel

get_or_create bug

catroot opened this issue · 1 comments

With model Order taken from examples:

my_order = Order.get_or_create(customer='Andrei', shop='GitHub')
my_order
(<Order: 660c3f24-e897-460b-a4e3-40587cdbda41>, True)
my_order['customer']
Traceback (most recent call last):
File "", line 1, in
TypeError: tuple indices must be integers or slices, not str

In case of create returns a plain tuple instead of model object.

This is not a bug. get_or_create returns a tuple by definition. Note the API documentation says:

get_or_create(id_=None, **kwargs)
Tries to retrieve a single record, filtered either by an id_ or by kwargs. If it succeeds, it returns a tuple like (obj, False). If it fails, it creates the object and returns a tuple like (obj, True).

...as such, you should call get_or_create by unpacking it into a tuple like so:

(my_order, is_newly_created) = Order.get_or_create(customer='Andrei', shop='GitHub')

If the order is found and fetched, is_newly_created --> False, else if it's created it'll be True.