我正在创建一个允许用户回答表单的应用。
所以我有:
class Form(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
topic = models.CharField(max_length=40)
class Question(models.Model):
form = models.ForeignKey(Form, on_delete=models.CASCADE)
text = models.CharField(max_length=200)
question_type = models.CharField(max_length=10)
class Participation(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
form = models.ForeignKey(Form, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
class Answer(models.Model):
participation = models.ForeignKey(Participation)
question = models.ForeignKey(Question)
value = models.CharField(max_length=255)
其中User
是来自django.contrib.auth.models
如何查询已回答特定表单的所有用户?
答案 0 :(得分:0)
如何查询已回答特定表单的所有用户?
如果你有一个特定的form
个实例,你可以获得所有答案,用户参与并回答了这些答案,并从中获取所有用户:
users = Answer.objects.filter(participation__form=form).values('participation__user')
如果您需要格外谨慎,可以查看问题中的表单是否与参与表格相同:
users = Answer.objects.filter(participation__form=form,
question__form=form).values('participation__user')