Django的查询多对多的关系
问题描述:
我有以下两种模式Django的查询多对多的关系
class Questionnaire(models.model)
name = models.CharField(max_length=128, null=True, blank=True)
type = models.CharField(max_length=128,choices=questionnaire_choices)
class TestPopulation(models.Model)
user = models.ForeignKey(User, blank=True, null=True)
age = models.CharField(max_length=20, blank=True, null=True)
education = models.CharField(max_length=50, blank=True, null=True,
choices=EDUCATION_CHOICES)
questionnaire = models.ManyToManyField(Questionnaire, blank=True, null=True)
现在我怎样才能问卷特定用户(登录用户)的数量。 ?
答
test_population = TestPopulation.objects.get(user=user)
test_population.questionnaire.all()
答
questionnaire.objects.filter(test_population__user=user).count()
他所需要的数量,以便:'test_population.questionnaire.count()' –