django使用mysql保存数据出错。

求教大神!在做django关于商城订单提交时出现错误:save() prohibited to prevent data loss due to unsaved related object 'order'.
实在找不到原因。。希望哪位大大可以帮下。。

详细截图:
图片描述

图片描述

视图:

class ShowOrder(View):

def get(self, request, oid):

print(u'hello')

order_main = OrderMain.objects.get(pk=oid)

address = DeliveryAddress.objects.filter(user=request.user).all()

order_detail = OrderDetail.objects.filter(order=order_main).all()

return render(request, 'shop_order/place_order.html', locals())

def post(self, request, oid):

order_main = OrderMain.objects.get(pk=oid)

order_detail = OrderDetail.objects.filter(order=order_main).all()

address_id = request.POST.get('address_id')

print(address_id)

address = DeliveryAddress.objects.get(pk=address_id)

try:

with transaction.atomic():

# 使用事务进行库存的删减,当其中一项出现错误时,则所有操作都将回原。

for detail in order_detail:

# 如果库存不足则跑出异常

if detail.count < detail.goods_info.stock:

raise StockException(u'%s库存不足' % detail.goods_info)

detail.goods_info.stock = F('stock') - detail.count

detail.goods_info.save()

order_main.receiver = address

order_main.is_pay = '1'

##############################

# 代码就是下面这个save出错的 #

##############################

order_main.save()

return

except StockException:

order_main.is_pay = '-1'

order_main.save()

message = u'已下单,5秒后跳转至付款界面,您也可以点击这里进行跳转'

return render(request, 'shop_order/message.html', locals())

数据库:

# 生成对应订单

class OrderMain(models.Model):

order_status = (

('-1', '取消'),

('0', '创建'),

('1', '未支付'),

('2', '已支付'),

('3', '代发货'),

('4', '已发货')

)

uuid = models.CharField(max_length=50, unique=True, verbose_name=u'订单编号')

order_time = models.DateTimeField(auto_now_add=True, verbose_name=u'订单提交时间')

pay_time = models.DateTimeField(null=True, blank=True, verbose_name=u'付款时间')

user = models.ForeignKey(UserProFiles, verbose_name=u'购买用户')

total = models.DecimalField(null=True, max_digits=10, decimal_places=2, verbose_name=u'总价格')

is_pay = models.CharField(max_length=5, choices=order_status, default='0', verbose_name=u'订单状态')

receiver = models.ForeignKey(DeliveryAddress, null=True, verbose_name=u'收货详情')

class Meta:

verbose_name = u'订单中心'

verbose_name_plural = verbose_name

ordering = ('-id',)

def __str__(self):

return str(self.user) + str(self.order_id)

# 订单详情信息

class OrderDetail(models.Model):

order = models.ForeignKey(OrderMain, verbose_name=u'订单')

goods_info = models.ForeignKey(GoodsInfo, verbose_name=u'商品')

goods_price = models.DecimalField(max_digits=10, null=True, decimal_places=2, verbose_name=u'商品价格')

goods_total = models.DecimalField(default=0, max_digits=10, decimal_places=2, verbose_name=u'商品总价')

count = models.IntegerField(verbose_name=u'购买商品数量')

class Meta:

verbose_name=u'订单详情'

verbose_name_plural = verbose_name

ordering =('-id',)

def __str__(self):

return str(self.order) + str(self.goods_info)

回答:

看你的逻辑是通的,我也试了代码可以跑。我猜测是数据库的问题,你的数据库外键关联没做on_delete=True,可能导致保存一个模型对象的时候,外键关联的需要值却没有,其它的看不出什么来了,只能帮你到这了。 话说这是公司代码?还是自己写了学习的。

回答:

试试下面这个:

try:

#do try

except:

#do except...

save() #in here.

以上是 django使用mysql保存数据出错。 的全部内容, 来源链接: utcz.com/a/159626.html

回到顶部