我将以下三种模型与多对多关系联系起来:
class TeamMember(models.Model):
member_name = models.CharField(max_length=140)
title = models.CharField(max_length=140, null=True, blank=True)
email = models.EmailField()
report = models.ManyToManyField(Report, through='Owner')
class Report(models.Model):
report_name = models.CharField(max_length=140)
report_description = models.TextField(null=True, blank=True)
last_updated = models.DateTimeField(null=True, blank=True)
metric = models.ManyToManyField(Metric, through='MetricAssociation')
class Owner(models.Model):
teammember = models.ForeignKey(TeamMember, on_delete=models.CASCADE)
report = models.ForeignKey(Report, on_delete=models.CASCADE)
我想构建一个TeamMembers的对象列表,该列表属于特定报告(由所有者指定)但是遇到了麻烦。目前我有报告的ID。我试过这个,但似乎错了:
owners = TeamMember.objects.filter(report__id=id).distinct().values_list('name', flat=True)
答案 0 :(得分:3)
name
模型中没有字段TeamMember
。您有member_name
而不是
owners = TeamMember.objects.filter(report_id=id).distinct().values_list('member_name', flat=True)
在旁注中,您可以使用report_id
代替需要加入操作的report__id
。