我有两个模型
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)
现在我如何获得特定用户(登录用户)的问卷数量。 ?
答案 0 :(得分:4)
test_population = TestPopulation.objects.get(user=user)
test_population.questionnaire.all()
答案 1 :(得分:2)
questionnaire.objects.filter(test_population__user=user).count()