私のプロジェクトで製品とカテゴリのカートリッジとメザニンを使用していて、カスタムモデルにManyToManyFieldを追加しようとしています。完全彼らのモデルの場合Django:サブクラス化されたオブジェクトのm2m
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"
私は何をしていますか? g間違っている?サブクラス化されたオブジェクトにm2mを追加することも可能ですか?カテゴリモデルを変更する必要がありますか?
返信いただきありがとうございます。 私は、BaseProduct m2mの "through = 'CategoryLink'"部分が両方を容易にすると考えられていたと思いましたか?その価値については、https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationshipsのガイドに従おうとしています。 –
時には、 m2mの関係での外来キーの明示的な宣言は問題を引き起こす可能性があります。モットーはそれを簡単に保つことですね、それじゃないの? – zaidfazil