【问题标题】:How to filter values from 3 different models in django如何在 django 中过滤来自 3 个不同模型的值
【发布时间】:2020-05-01 06:53:05
【问题描述】:

我有 3 个班级,我想根据 3 个班级的标准过滤它们。我对 django 非常陌生,尤其是模型。需要你的帮助。 Student、group 和 nondemandgroup 是我的 Db 中的表。

class Students():
    name=models.CharField(max_length=200)
    surname=models.CharField(max_length=200)

class Group20():
    name=models.CharField(max_length=200)
    studentss=models.ManyToManyField(Students)
    math=models.DecimalField(decimal_places=2,max_digits=1000)
    english=models.DecimalField(decimal_places=2,max_digits=1000)

class Nondemandgroup():
      name=models.CharField(max_length=200)
      studentss=models.ManyToManyField(Students)
      acting=models.DecimalField(decimal_places=2,max_digits=1000)  
      cooking=models.DecimalField(decimal_places=2,max_digits=1000)

我想获取最终成绩符合以下标准的学生列表:

final_grade = group20.objects.filter(math__gt=60, ((math+english+acting)/3)__gt=70)

actingnondemandgroup 类内,所以我的final_grade 不起作用,它说no such column as acting

如何让代理栏目发挥作用?我尝试了外键,但它不起作用并给出错误。

或者我应该创建一个新模型并在其中过滤我将创建 3 个模型的外键?

我很困惑,因为我对 Django 很陌生,模型也很混乱。探索了网络,但在我的情况下,我误解了 smth,公式不起作用。

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    让我们一步一步来。我们希望学生符合给定条件。

    1. 我们看到的第一件事是学生没有以任何方式连接到小组。因此,让我们将学生与小组联系起来

      from django.db import models
      
      class Student(models.Model):
          name=models.CharField(max_length=200)
          surname=models.CharField(max_length=200)
      
      class Group20(models.Model):
          student = models.ForeignKey(Student, on_delete=models.CASCADE)
          name=models.CharField(max_length=200)
          math=models.DecimalField(decimal_places=2,max_digits=1000)
          english=models.DecimalField(decimal_places=2,max_digits=1000)
      
      class Nondemandgroup(models.Model):
          student = models.ForeignKey(Student, on_delete=models.CASCADE)
          name=models.CharField(max_length=200)
          acting=models.DecimalField(decimal_places=2,max_digits=1000)  
          cooking=models.DecimalField(decimal_places=2,max_digits=1000)
      
    2. 现在让我们进入计算最终成绩的部分

      让我们从 group20 模型中获取所有数学超过 60 的用户。我们还需要数学和英语来计算平均分以及表演。因此,让我们也获得该值。

      group20_objs = Group20.objects.filter(math__gt=60).values_list('student__name', 'math', 'english')
      

      这将给出以下格式的值

      [("studentA", "math_score_of_A", "english_score_of_A"), ...]
      [("Chantel", "100", "100"), ("Chantel1", "90", "85"),..]
      

      现在让数学、英语和表演平均分在70以上的学生。

      final_students = []
      for item in group20_objs:
          student_name, math_score, english_score = item
          # Get acting score for user
          # Hoping student will have record here as well
          acting_score = Nondemandgroup.objects.filter(student__name=student_name).first(). acting
          if (acting_score + math_score + english_score)/3 >= 70:
              final_students.append(student_name)
      

    现在 final_students 列表将包含成绩高于 70 的学生姓名。如果需要,请尝试混合模型。

    另外我建议通过django models docs 更好地理解

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 2021-01-20
    • 2011-12-03
    相关资源
    最近更新 更多