2012-03-09 5 views
1

У меня есть две модели в Django для 2d игры в карты на основе:Multiple M2M в Джанго

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) 

Теперь я просто хочу связать эти две модели, используя одну таблицу:

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 поле Джанго говорит:

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

ответ

3

Проблема в том, что на модели Block она пытается создать обратные ссылки. Django называет те ссылки на основе модели, в вашем случае для всех ManyToMany она пытается создать shopbuilding_set и имена сталкиваются. Чтобы избежать этого добавить related_name, например:

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") 

Тогда вы будете иметь возможность получить доступ к ShopBuilding из Block например так:

block.shopbuilding_npc_set.all()

+0

да спасибо, но как я могу убедиться, что эти поля используя ту же промежуточную модель? – est

+0

Если вы хотите, чтобы одна промежуточная модель не создавала 5 отношений ManyToMany, вы создаете только одну. например block = models.ManyToManyField (Block), и все. Проблема в том, что когда у вас есть одна промежуточная модель, у вас не может быть «типов» –

+0

, так что в принципе Django ORM не поддерживает несколько одинаковых m2m с одной ассоциативной моделью? – est