2017-05-03 8 views
0

私のプロジェクトで製品とカテゴリのカートリッジとメザニンを使用していて、カスタムモデルに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を追加することも可能ですか?カテゴリモデルを変更する必要がありますか?

答えて

0

カテゴリとm2m関係のモデルBaseProductがあります。

モデルCategoryLinkは、そのm2m関係の繰り返しです。 その禅の禅、あなたは違反しています。 "あなたのことをやめてください"。 m2mフィールドまたはマッピングテーブル(CategoryLink)を削除します。 うまくいくはずです。

m2mフィールドではなくマッピングテーブルを使用することをお勧めします。 私の意見では、より機敏です。

あなたが尋ねたことを願っています。

+0

返信いただきありがとうございます。 私は、BaseProduct m2mの "through = 'CategoryLink'"部分が両方を容易にすると考えられていたと思いましたか?その価値については、https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationshipsのガイドに従おうとしています。 –

+0

時には、 m2mの関係での外来キーの明示的な宣言は問題を引き起こす可能性があります。モットーはそれを簡単に保つことですね、それじゃないの? – zaidfazil

関連する問題