質問

2DマップベースのゲームのためのDjangoに2つのモデルがあります:

class Block(models.Model):
   type = models.IntegerField()

class ShopBuilding(models.Model):
   house_blocks = models.ManyToManyField(Block)
   street_blocks = models.ManyToManyField(Block)
   river_blocks = models.ManyToManyField(Block)
   decoration_blocks = models.ManyToManyField(Block)
   npc_blocks = models.ManyToManyField(Block)
.

今度は、1つのテーブルを使用してこれら2つのモデルを関連付けるだけです。

class ShopBlockAssoc(models.Model):
    block = models.ForeignKey(Block)
    shop = models.foreignKey(Shop)
.

throughモデルでShopBuildingフィールドを設定した後、Django YILED倍数はのように、syncdbのときに失敗します。

    Error: One or more models did not validate:
tnew.shopbuilding: Accessor for m2m field 'house_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'house_blocks'.
tnew.shopbuilding: Accessor for m2m field 'house_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'house_blocks'.
tnew.shopbuilding: Accessor for m2m field 'house_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'house_blocks'.
tnew.shopbuilding: Accessor for m2m field 'house_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'house_blocks'.
tnew.shopbuilding: Accessor for m2m field 'street_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'street_blocks'.
tnew.shopbuilding: Accessor for m2m field 'street_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'street_blocks'.
tnew.shopbuilding: Accessor for m2m field 'street_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'street_blocks'.
tnew.shopbuilding: Accessor for m2m field 'street_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'street_blocks'.
tnew.shopbuilding: Accessor for m2m field 'river_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'river_blocks'.
tnew.shopbuilding: Accessor for m2m field 'river_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'river_blocks'.
tnew.shopbuilding: Accessor for m2m field 'river_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'river_blocks'.
tnew.shopbuilding: Accessor for m2m field 'river_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'river_blocks'.
tnew.shopbuilding: Accessor for m2m field 'decoration_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'decoration_blocks'.
tnew.shopbuilding: Accessor for m2m field 'decoration_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'decoration_blocks'.
tnew.shopbuilding: Accessor for m2m field 'decoration_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'decoration_blocks'.
tnew.shopbuilding: Accessor for m2m field 'decoration_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'decoration_blocks'.
tnew.shopbuilding: Accessor for m2m field 'npc_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'npc_blocks'.
tnew.shopbuilding: Accessor for m2m field 'npc_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'npc_blocks'.
tnew.shopbuilding: Accessor for m2m field 'npc_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'npc_blocks'.
tnew.shopbuilding: Accessor for m2m field 'npc_blocks' clashes with related m2m field 'Block.shopbuilding_set'. Add a related_name argument to the definition for 'npc_blocks'.
.

db_tableフィールドを設定した場合、Djangoは次のとおりです。

The model has two manually-defined m2m relations through the model, which is not permitted. Please consider using an extra field on your intermediary model instead.
.

どうすれば修正できますか?私は、それらのショップブロック関係が同じDB_TABLE を持つ同じ中間アソシエイトモデルの共有を使用していることを確認したいです。

役に立ちましたか?

解決

The problem is that on model Block it tries to create backwards links. Django names those links based on Model, in your case for all ManyToMany it tries to create shopbuilding_set and names collide. To avoid this add related_name, for example:

class ShopBuilding(models.Model):
   house_blocks = models.ManyToManyField(Block, related_name="shopbuilding_house_set")
   street_blocks = models.ManyToManyField(Block, related_name="shopbuilding_street_set")
   river_blocks = models.ManyToManyField(Block, related_name="shopbuilding_river_set")
   decoration_blocks = models.ManyToManyField(Block, related_name="shopbuilding_decoration_set")
   npc_blocks = models.ManyToManyField(Block, related_name="shopbuilding_npc_set")

Then you'll be able to access ShopBuilding from Block instance like this:

block.shopbuilding_npc_set.all()

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top