2011-11-27 171 views
0

我有以下两种模式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) 

现在我怎样才能问卷特定用户(登录用户)的数量。 ?

回答

1
test_population = TestPopulation.objects.get(user=user) 
test_population.questionnaire.all() 
+3

他所需要的数量,以便:'test_population.questionnaire.count()' –

0
questionnaire.objects.filter(test_population__user=user).count()