我在 Django 中有两个模型用于基于 2d 地图的游戏:

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 在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 共享相同的中间关联模型

有帮助吗?

解决方案

问题是在模型上 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")

然后您就可以访问 ShopBuildingBlock 像这样的实例:

block.shopbuilding_npc_set.all()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top