已阅读多个解决方案的来源,例如Guaravs帖子(包括评论中的所有建议)和Alexander,仅举几例,但仍无法解决问题。创建数据库条目@ models.OrderItemTax.objects.create()
时出错使用django 1.11.3和MySQL 14.14
views.py **只有部分代码,因为函数超过600行。
def checkout_cart(request):
try:
item_to_group_keys = []
promo_key = 'promo:' + str(request.user.id)
with connection.cursor() as cursor:
with transaction.atomic():
// more code here...
try:
with transaction.atomic(): // updated solution
event = ''
group = models.Group.objects.get(id=order_item.group.id)
now = timezone.now()
event_date = now.strftime('%Y-%m-%d')
try:
event = models.Event.objects.get(group_id=group.id, date=event_date)
try:
self_attendee = models.EventAttendee.objects.get(event=event,
user_id=request.user.id,
is_attending=True)
except:
self_attendee = models.EventAttendee.objects.create(event=event,
user_id=request.user.id,
is_attending=True)
self_attendee.save()
except models.Event.DoesNotExist:
event = models.Event.objects.create(group_id=group.id, date=event_date, planner_id=request.user.id)
todays_site_deal = models.SiteDeal.objects.get_deal()
if todays_site_deal is not None and todays_site_deal != '':
event.site_deal = todays_site_deal
event.save()
self_attendee = models.EventAttendee.objects.create(event=event, user_id=request.user.id, is_attending=True)
self_attendee.save()
initial_group_members = models.GroupMembership.objects.filter(group_id=group.id, status_id=1)
group_members = initial_group_members.exclude(user_id=request.user.id)
for gm in group_members:
userprofile = models.UserProfile.objects.get(user_id=gm.user_id)
attendee_request = models.EventAttendeeRequest.objects.create(event=event, user_id=gm.user_id)
attendee_request.status = models.EventRequestStatusOpt.objects.get(id=3)
attendee_request.save()
group_fn = gm.user.first_name
group_ln = gm.user.last_name
group_pn = userprofile.phone
if group_fn != '' and group_ln != '' and group_pn != '' and group_pn is not None:
non_digits = re.compile(r'[^\d]+')
group_pn = non_digits.sub('', group_pn)
# request_hash = str(member_request.pk) + str(group_id) + str(int(round(time.time())))
# request_hash = int(request_hash) ^ 0xABCEEFAB
# member_request.hash = request_hash
text_message = event.planner.first_name + " from your Cliiique " + event.group.name + " started shopping. Join today's event!"
try:
pn_check = client_lookup.phone_numbers.get(group_pn)
if pn_check.phone_number is not None: # if invalid, throws an exception
# SMS
# -------------------------------------------------
message = client_rest.messages.create(
body=text_message,
to=group_pn,
from_="+12132050074")
# -------------------------------------------------
except TwilioRestException as e:
pass
except: #group not in checkout, do not create event
pass
# Get tax amount for each Item
rate = Decimal('0.0950')
oi_price = Decimal(order_item.op_price.strip(''))
tax_amount = oi_price * rate
oi_tax = models.OrderItemTax.objects.create(amount=tax_amount,
rate=rate,
order_item_id=order_item.pk)
oi_tax.save()
except IntegrityError:
return HttpResponse("<strong>CODE #700: Fatal Transaction Error! Please contact customer service.</strong>")
答案 0 :(得分:2)
来自docs:
避免在原子内部捕获异常!
由于该函数具有多个嵌套的try和except,因此我在try中插入了另一个transaction.atomic()
,除了最接近发生错误的位置。
每个文档都嵌套了try,但excepts可能会使初始原子复杂化。我发现(第二次)放置原子会有所帮助。希望这可以帮助其他面临类似情况的人。
try:
item_to_group_keys = []
promo_key = 'promo:' + str(request.user.id)
with connection.cursor() as cursor:
with transaction.atomic():
// more code here...
try:
with transaction.atomic(): // **added second atomic**
//more code
except:
pass
except IntegrityError: