解决多对多关系的问题

时间:2019-02-16 16:17:31

标签: python-3.x django-models

我有这些模型,例如产品,订单和order_lines。订单模型通过称为产品的ManyToManyField链接到产品模型。 这是模型。 但是,当我运行makemigrations and migration命令时,无法在表顺序中看到现场产品。我删除了两次数据库,删除了迁移文件,然后重新运行迁移,仍然遇到相同的错误。我修改了模型订单并再次运行,但出现以下错误:  “通过=在M2M字段上)”%(old_field,new_field) ValueError:无法将字段page.Orders.products更改为pages.Orders.products-它们是不兼容的类型(您不能在M2M字段之间来回更改,也不能在M2M上添加或删除through = 字段)

class StatusCode(models.Model):
    """
    The StatusCode model represents the status of an order in the system.
    This field is intended to function as a tracking system.
    """
    short_name = models.CharField(max_length=10)
    name = models.CharField(max_length=300)
    description = models.TextField()


class Orders(models.Model):
    """
    The Order model represents a customer order. It includes a
    ManyToManyField of products the customer is ordering and stores
    the date and total price information.
    """
    customer= models.ForeignKey(User, on_delete=models.CASCADE)
    status_code = models.ForeignKey(StatusCode, on_delete=models.CASCADE)
    orderDate = models.DateTimeField()
    total_price = models.DecimalField(max_digits=7, decimal_places=2)
    comment=models.TextField(blank=True)
    products=models.ManyToManyField(Product, blank=True, through='Order_lines')

class Order_lines(models.Model):
    """
    The Order_line model represents information about a specific product ordered by a customer.
    """
    order = models.ForeignKey(Orders, on_delete=models.CASCADE)
    product=models.ForeignKey(Product, on_delete=models.CASCADE)
    unit_price = models.DecimalField(max_digits=7, decimal_places=2)
    total_price = models.DecimalField(max_digits=7, decimal_places=2)
    quantity = models.PositiveIntegerField()
    comments = models.TextField(blank=True)

这是餐桌顺序的屏幕截图。

Orders table in mysql

请问这是什么错误?

0 个答案:

没有答案