这就是我工作的模式:
from django.db import models
from django.conf import settings
from products.models import Variation
class CartItem(models.Model):
cart = models.ForeignKey('Cart')
items = models.ForeignKey(Variation)
quantity = models.PositiveIntegerField(default=1)
def __unicode__(self):
return self.items.title
class Cart(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
items = models.ManyToManyField(Variation, through='CartItem')
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return (str(self.id))我以前运行过一次makemigrations和migrate,模型Cart中的items字段是items = models.ManyToManyField(CartItem)
现在,在进行此更改后,我得到以下错误:
ValueError: Cannot alter field carts.Cart.items into carts.Cart.items - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)我该怎么解决这个问题?请帮帮忙。
发布于 2016-04-18 18:19:54
就像错误说的那样,你不能把一个多到多转换成一个外键。您必须将其拆分为两个迁移:首先,完全删除原始字段并运行makemigrations来创建DROP列调用;然后,添加外键并再次运行makemigrations来创建add列。
https://stackoverflow.com/questions/36700782
复制相似问题