model - Multiple M2M in django -
i have 2 models in django 2d map based game:
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)
now want associate these 2 models using 1 table:
class shopblockassoc(models.model): block = models.foreignkey(block) shop = models.foreignkey(shop)
after set through
field in shopbuilding
model, django yiled multiple fails when syncdb, like
error: 1 or more models did not validate: tnew.shopbuilding: accessor m2m field 'house_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'house_blocks'. tnew.shopbuilding: accessor m2m field 'house_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'house_blocks'. tnew.shopbuilding: accessor m2m field 'house_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'house_blocks'. tnew.shopbuilding: accessor m2m field 'house_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'house_blocks'. tnew.shopbuilding: accessor m2m field 'street_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'street_blocks'. tnew.shopbuilding: accessor m2m field 'street_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'street_blocks'. tnew.shopbuilding: accessor m2m field 'street_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'street_blocks'. tnew.shopbuilding: accessor m2m field 'street_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'street_blocks'. tnew.shopbuilding: accessor m2m field 'river_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'river_blocks'. tnew.shopbuilding: accessor m2m field 'river_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'river_blocks'. tnew.shopbuilding: accessor m2m field 'river_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'river_blocks'. tnew.shopbuilding: accessor m2m field 'river_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'river_blocks'. tnew.shopbuilding: accessor m2m field 'decoration_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'decoration_blocks'. tnew.shopbuilding: accessor m2m field 'decoration_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'decoration_blocks'. tnew.shopbuilding: accessor m2m field 'decoration_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'decoration_blocks'. tnew.shopbuilding: accessor m2m field 'decoration_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'decoration_blocks'. tnew.shopbuilding: accessor m2m field 'npc_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'npc_blocks'. tnew.shopbuilding: accessor m2m field 'npc_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'npc_blocks'. tnew.shopbuilding: accessor m2m field 'npc_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'npc_blocks'. tnew.shopbuilding: accessor m2m field 'npc_blocks' clashes related m2m field 'block.shopbuilding_set'. add related_name argument definition 'npc_blocks'.
if set db_table
field django says:
the model has 2 manually-defined m2m relations through model, not permitted. please consider using field on intermediary model instead.
how fix this? want make sure shop-block relations using sharing same intermediate associate model same db_table
the problem on model block
tries create backwards links. django names links based on model, in case manytomany tries create shopbuilding_set
, names collide. avoid add related_name
, 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 able access shopbuilding
block
instance this:
block.shopbuilding_npc_set.all()
Comments
Post a Comment