Django多对多关系“通过”

时间:2014-01-24 14:44:10

标签: python django

我正在建造一个有多件装备的模型。每个部分都是一个模型,它们之间存在ManyToMany关系。

class Station(models.Model):
    name = models.CharField

class Sensor(models.Model):
    name = models.CharField
    brand = models.CharField
    model = models.CharField
    station = models.ManyToManyField(Station, through='Relationship')

class Digitalizer(models.Model):
    name = models.CharField
    brand = models.CharField
    model = models.CharField
    station = models.ManyToManyField(Station, through='Relationship2')

class Relationship(models.Model):

    station = models.ForeignKey(Station)
    sensor = models.ForeignKey(Sensor)
    setting_up_date = models.DateField()
    removal_date = models.DateField()

由于我想保存一些关于设备中包含的部分的历史信息,我想使用中间模型或关系来保存有关安装和删除部件的日期的信息(类似于设备的成员示例) doc https://docs.djangoproject.com/en/dev/topics/db/models/

我的问题是,我是否可以为需要与传感器模型关联的2个或更多模型使用相同的“关系”?或者我是否需要为每个模型复制关系?

像这样的东西

class Relationship(models.Model):

    station = models.ForeignKey(Station)

    sensor = models.ForeignKey(Sensor)
    digitalizer = models.ForeignKey(Digitalizer)
    foo = models.ForeignKey(Foo)
    setting_up_date = models.DateField()
    removal_date = models.DateField()

0 个答案:

没有答案