Django OneToOneField在克隆过程中不允许空值

时间:2014-07-31 00:51:12

标签: django

假设我有以下型号:

class Cart(models.Model):
  uuid = models.CharField()

class Order(models.Model):
  cart = models.OneToOneField('Cart')

当我想要克隆购物车时:

cart.id = None
cart.order = None
cart.save()

我收到此错误:

ValueError: Cannot assign None: "Cart.order" does not allow null values.

1 个答案:

答案 0 :(得分:3)

试试这个:

class Cart(models.Model):
    uuid = models.CharField()

class Order(models.Model):
    cart = models.OneToOneField('Cart',blank=True,null=True)