Python Django:该模型通过中间模型具有两个相同的多对多关系
我看到这个问题: 但我认为我的问题略有不同:Python Django:该模型通过中间模型具有两个相同的多对多关系,python,django,Python,Django,我看到这个问题: 但我认为我的问题略有不同: class Building(models.Model): name = models.CharField(max_length=64) prices = models.ManyToManyField('Resource', through='BuildingResourceRelation',
class Building(models.Model):
name = models.CharField(max_length=64)
prices = models.ManyToManyField('Resource',
through='BuildingResourceRelation',
related_name="prices")
outputs = models.ManyToManyField('Resource',
through='BuildingResourceRelation',
related_name="outputs")
inputs = models.ManyToManyField('Resource',
through='BuildingResourceRelation',
related_name="inputs")
class Resource(models.Model):
name = models.CharField(max_length=64)
class BuildingResourceRelation(models.Model):
building = models.ForeignKey('Building', on_delete=models.CASCADE)
resource = models.ForeignKey('Resource', on_delete=models.CASCADE)
amount = models.PositiveIntegerField()
根据设计,可以有不同数量的价格、投入和产出
我可以通过每个模型创建,但没有更好的方法吗