Django ManyToMany关系包含值

时间:2013-04-16 14:09:25

标签: django django-models many-to-many relationship django-orm

我正在尝试搜索ManyToMany关系,但我做错了,我不确定它是什么

t = Teacher.objects.get(username=u.username)
var = list(Allowed.objects.filter(allowed=t))

这是我正在使用的课程:

class Allowed(models.Model):
  owner = models.ForeignKey(Teacher, related_name='allowed_teacher')
  reason = models.CharField(max_length=200, blank=True, null=True)
  allowed = models.ManyToManyField(Teacher, related_name='who_is_allowed', blank=True, null=True)

我该怎么做?

修改 Teacher没有对Allowed

的任何引用

1 个答案:

答案 0 :(得分:1)

您可以尝试:

t = Teacher.objects.get(username=u.username)
var = t.allowed_set.all()