简体   繁体   English

在Django中基于ForeignKeys的查询集

[英]Queryset based on ForeignKeys in Django

While learning Django I am creating an example app in which I have two types of users; 在学习Django时,我正在创建一个示例应用程序,其中有两种类型的用户: students and educators. 学生和教育者。 Educators can create Study sections and students can choose which they would like to participate in (just a BooleanField yes or no). 教育者可以创建“学习”部分,学生可以选择自己想参加的部分(只是BooleanField是或否)。 I would like to create a "view participants" page for each study so that the educators can see which students are participating in each section. 我想为每个研究创建一个“查看参与者”页面,以便教育者可以看到每个部分都在参加哪些学生。 To do this for each study section I need to query all of the student users who marked "yes" to participate in that study section. 为此,我需要查询所有标记为“是”的学生用户,以参加该学习部分。 I am a bit stuck on how to use Django's QuerySet methods to do this. 我对如何使用Django的QuerySet方法执行此操作有些困惑。

Here is my models.py 这是我的models.py

class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_educator = models.BooleanField(default=False)


class Interest(models.Model):
...


class Study(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='studies')
    name = models.CharField(max_length=255)
    interest = models.ForeignKey(Interest, on_delete=models.CASCADE, related_name='studies')

    def __str__(self):
        return self.name


class Detail(models.Model):
...


class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    interests = models.ManyToManyField(Interest, related_name='interested_students')

    def get_details(self, study):
        details = study.details.all().order_by('start_date')
        return details

    def __str__(self):
        return self.user.username


class StudentAnswer(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='study_answers')
    study = models.ForeignKey(Study, on_delete=models.CASCADE, related_name='study_participate', null=True)
    participate = models.BooleanField('Correct answer', default=False)

I would like to write a views.py function like this: 我想这样编写一个views.py函数:

@method_decorator([login_required, educator_required],     name='dispatch')
class StudyResultsView(DetailView):
    model = Study
    context_object_name = 'study'
    template_name = 'classroom/educators/study_results.html'

    def get_context_data(self, **kwargs):
        study = self.get_object()
        participants = study.????
        total_participants = participants.count()
        extra_context = {
            'participants': participants,
            'total_participants': total_participants,
        }
        kwargs.update(extra_context)
        return super().get_context_data(**kwargs)

    def get_queryset(self):
        return self.request.user.studies.all()

But I cannot figure out what the correct query should be at participants = study.???? 但是我无法弄清楚participants = study.????应该是什么正确的查询participants = study.???? in order to select all of the users who marked participate=True for that study. 为了选择标记为该研究为True的所有用户。

participants = study.objects.filter(study_participate__participate = True)

这肯定会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM