0
我使用的墨盒和夹层在我的项目产品和品类,我想一个ManyToManyField添加到我的自定义模型上的M2M。Django的:一个子类的对象
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?我需要修改类别模型吗?
感谢您的回复。 我认为BaseProduct m2m的“through ='CategoryLink'”部分应该有利于两者?对于我试图按照https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships –
中的指南,有时候,在m2m关系中外键的明确声明会导致麻烦。格言是保持简单,是不是!!? – zaidfazil