【发布时间】:2017-05-03 09:11:53
【问题描述】:
我在我的项目中为产品和类别使用墨盒和夹层,并且我正在尝试将 ManyToManyField 添加到我的自定义模型中。
from cartridge.shop.models import Product, Category
class BaseProduct(Product):
(...)
related_categories = models.ManyToManyField(Category, blank=True, through='CategoryLink')
class CategoryLink(models.Model):
category = models.ForeignKey(Category)
product = models.ForeignKey(BaseProduct)
为了完整起见,他们的模型是:
分类:https://github.com/stephenmcd/cartridge/blob/master/cartridge/shop/models.py#L341
产品:https://github.com/stephenmcd/cartridge/blob/master/cartridge/shop/models.py#L105
但是,当我尝试执行迁移时,这给了我以下错误:
Operations to perform:
Apply all migrations: admin, auth, blog, brochures, case_studies, conf, contenttypes, core, django_comments, forms, galleries, generic, mezzanine_blocks, pages, quotes, redirects, services, sessions, shop, sites, stevensons_shop, stevensons_user, twitter, utilities
Running migrations:
Applying stevensons_shop.0057_baseproduct_related_categories...Traceback (most recent call last):
File "/home/vagrant/virtualenvs/mezzanine/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: there is no unique constraint matching given keys for referenced table "stevensons_shop_baseproduct"
我做错了什么?甚至可以在子类对象上添加 m2m 吗?我需要对 Category 模型进行修改吗?
【问题讨论】:
标签: python django many-to-many