简体   繁体   English

Django 过滤外查询键

[英]Django filter Foreign query Key

In this quiz game, I'm trying to filter the questions of a particular course.在这个问答游戏中,我试图过滤特定课程的问题。

models.py

class Course(models.Model):
    course_name = models.CharField(max_length=50)
    date_created = models.DateTimeField(auto_now_add=True)

class Quiz(models.Model):
    course_name = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='quiz_course')
    question = models.TextField(max_length=100)

class Choice(models.Model):
    question = models.ForeignKey(Quiz, on_delete=models.CASCADE, related_name="choice_question")
    choice = models.CharField(max_length=100)
    is_true = models.BooleanField("This is Correct Answer", default=False)

class Quizgame(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    total_score = models.DecimalField("Total score", default=0, decimal_places=2, max_digits=6)
    start_time = models.DateTimeField(auto_now_add=True)
    finish_time = models.DateTimeField(null=True)
    
    def get_new_question(self,course_id):
        used_question = AttemptedQuestion.objects.filter(quiz_profile=self).values_list('question__id',flat=True)
        remaining_questions = Quiz.objects.exclude(id__in=used_question)
        if not remaining_questions.exists():
            return
        return random.choice(remaining_questions)
    
    def create_attempt(self, question):
        attempted_question = AttemptedQuestion(question=question, quiz_profile=self)
        attempted_question.save()

class AttemptedQuestion(models.Model):
    quiz_profile = models.ForeignKey(Quizgame, on_delete=models.CASCADE, related_name='attempt')
    question = models.ForeignKey(Quiz, on_delete=models.CASCADE)
    choice = models.ForeignKey(Choice, on_delete=models.CASCADE, null=True)
    is_true = models.BooleanField(default=False)

In above where in class Quizgame I filter the questions in get_new_question method here I passed an course_id as an argument by I don't know how to filter out with the course_id for the particular course questions..在上面的 class Quizgame 中,我在这里过滤了get_new_question方法中的问题,我通过了 course_id 作为参数,因为我不知道如何用 course_id 过滤掉特定的课程问题。

If you are trying to link a question to a course, I can't see a ForeignKey related to a Course object in your AttemptedQuestion class.如果您尝试将问题链接到课程,我在您的 AttemptedQuestion class 中看不到与课程 object 相关的外键。 You should add你应该添加

class AttemptedQuestion(models.Model):
    # ...
    course = models.ForeignKey(Course, on_delete=models.CASCADE, null=True)

and then you can simply filter by然后你可以简单地过滤

qs = AttemptedQuestion.objects.filter(course=course_id)

Before answering your question there is a simple tip that will help you raise the readability of your code.在回答您的问题之前,有一个简单的提示可以帮助您提高代码的可读性。

If you're creating FK to a model named Course , name the field course .如果要为名为Course的 model 创建 FK,请将字段命名为course "course_name" is implying we will get a name in that field. “course_name”意味着我们将在该字段中获得一个名称。

To access all the Quiz instances by course_id variable, you can use the __ operator in filter() method like this:要通过course_id变量访问所有Quiz实例,您可以在 filter() 方法中使用__运算符,如下所示:

Quiz.objects.filter(course_name__id=course_id)

Note the double underscore __ after the "course_name".请注意“课程名称”后面的双下划线__ What this does is telling Django: "Filter Quiz where course_name's id is equal to 'course_id'".这样做是告诉 Django:“在 course_name 的 id 等于 'course_id' 的情况下筛选测验”。

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

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