如何在Django中将一个表的行链接到另一表的行?

时间:2019-10-20 00:30:07

标签: django django-models django-queryset

我必须有四个表:

class Instrument(models.Model):
    name = models.CharField(max_length=100, null=True, blank=True)
    instrumentation = models.ManyToManyField(Instrumentation, verbose_name=_('instrumentation'), related_name='instrument', blank=True)

class Instrumentation(models.Model):
    name = models.CharField(max_length=100, null=True, blank=True)

该表的条目是:

row 1: name = violin, instrumentation = piano trio
row 2: name = piano, instrumentation = piano trio
row 3: name = cello, instrumentation = piano trio

这是另外两个表:

class Ensemble(models.Model):
    name = models.CharField(max_length=200, null=True, blank=True)

class EnsembleMember(models.Model):
    ensemble = models.ForeignKey(Ensemble, verbose_name=_('ensemble'), related_name='ensemble_member', on_delete=models.PROTECT)
    member = models.ForeignKey(Person, verbose_name=_('member'), null=True, blank=True, on_delete=models.PROTECT)

row 1: name = person 1 (violinist), ensemble = Beau Arts Trio
row 2: name = person 2 (pianist), ensemble = Beau Arts Trio
row 3: name = person 3 (cellist), ensemble = Beau Arts Trio

如何将Instrument与EnsembleMember链接? ForeignKey或ManyToManyField?

一种映射到许多不同的乐团成员的乐器。

row 1 in Instrument to row 1 in Ensemble Member
row 2 in Instrument to row 2 in Ensemble Member
row 3 in Instrument to row 3 in Ensemble Member

并且在admin.py中有下拉选项

0 个答案:

没有答案