Форум сайта python.su
Здраствуйте! Никак не могу понять как работает етот метод. Доку прочитал, но никак не удаеться ето сделать.
Суть задачи, нужно создать обьект, если его не существует, и редактировать если он существует:
obj, created=Products.objects.update_or_create(art=art, product_name=product_name, cost=cost, packing=1, profile=request.user, group=group, defaults=dict(product_name=product_name, cost=cost))
Отредактировано eboome (Янв. 29, 2015 17:55:49)
Офлайн
The update_or_create method tries to fetch an object from database based on the given kwargs. If a match is found, it updates the fields passed in the defaults dictionary.
Офлайн
inoksну по факту у меня создаеться новая запись.
The update_or_create method tries to fetch an object from database based on the given kwargs. If a match is found, it updates the fields passed in the defaults dictionary.
Офлайн
Это потому что ты всегда пытаешься достать запись с новыми параметрами
obj, created=Products.objects.update_or_create(art=art, product_name=product_name, cost=cost, packing=1, profile=request.user, group=group, defaults=dict(product_name=product_name, cost=cost))
obj, created=Products.objects.update_or_create(art=art, packing=1, profile=request.user, group=group, defaults=dict(product_name=product_name, cost=cost)
Отредактировано PanovSergey (Янв. 29, 2015 22:42:37)
Офлайн
PanovSergey
Это потому что ты всегда пытаешься достать запись с новыми параметрамиobj, created=Products.objects.update_or_create(art=art, product_name=product_name, cost=cost, packing=1, profile=request.user, group=group, defaults=dict(product_name=product_name, cost=cost))
Офлайн