在 Django 中使用不同的方法时如何使用过滤数据? [英] How to use filtering data while using distinct method in django?

查看:15
本文介绍了在 Django 中使用不同的方法时如何使用过滤数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我解决我的问题我希望我的标题足以理解我的意思,请帮助我解决这个问题.

please help me on my problem I hope my title is enough to understand what I mean, please help me on this problem guys.

当我尝试这个时:

id_list = grade.objects.filter(Teacher=m.id).values_list('Students_Enrollment_Records_id',flat=True).distinct()

我使用 distinct() 从查询结果中消除了学生注册记录的重复行,但我想知道为什么结果是这样的:

I use distinct() to eliminates duplicate rows of Students Enrollment Record from the query results but I wonder why the result is like this:

我应该怎么做才能在我的 html 中显示学生姓名而不是 QuerySet?

What should I do to show the Students name not that QuerySet in my html?

这是我当前的views.py:

id_list = grade.objects.filter(Teacher=m.id).values_list('Students_Enrollment_Records_id',flat=True).distinct()
print(id_list)
grades = grade.objects.filter(Students_Enrollment_Records_id__in=id_list)
print(grades)

这是我的models.py:

class grade(models.Model):
    Teacher = models.ForeignKey(EmployeeUser, related_name='+', on_delete=models.CASCADE,
                                null=True, blank=True)
    Grading_Categories = models.ForeignKey(gradingCategories, related_name='+', on_delete=models.CASCADE,
                                           null=True, blank=True)
    Subjects = models.ForeignKey(Subject, related_name='+', on_delete=models.CASCADE, null=True)
    Students_Enrollment_Records = models.ForeignKey(StudentsEnrolledSubject, related_name='+',
                                                    on_delete=models.CASCADE, null=True)
    Average = models.FloatField(null=True, blank=True)

更新

当我尝试这个时

piste = grade.objects.filter(Teacher_id=m.id).values_list('Students_Enrollment_Records').annotate(Average=Avg('Average')).order_by('Grading_Categories').distinct()

计算是固定的,但是没有显示老师姓名,科目和学生姓名,但ID是这样显示的

the computation is fix but the teacher name, Subject and Name of students didn't display but the ID is display just like this

这是我想要的答案

这就是我在 html 中发帖的方式

this is how I post in html

views.py

return render(request, 'Homepage/index.html', {"piste":piste})

html

{% for n in piste %}
          <tr>
              <td>{{n.Teacher}}</td>
              <td>{{n.Subjects}}</td>
              <td>{{n.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}</td>
              <td>{{n}}</td>
          </tr>
          {% endfor %}

这是model.py

This is model.py

class EmployeeUser(models.Model):
    Image = models.ImageField(upload_to='images', null=True, blank=True)
    Employee_Number = models.CharField(max_length=500, null=True)
    Username = models.CharField(max_length=500, null=True)
    Password = models.CharField(max_length=500, null=True)
    My_Department = models.ForeignKey(Department, related_name='+', on_delete=models.CASCADE, blank=True)
    My_Position = models.ForeignKey(Position, related_name='+', on_delete=models.CASCADE, blank=True)
    Firstname = models.CharField(max_length=500, null=True)
    Middle_Initial = models.CharField(max_length=500, null=True)
    Lastname = models.CharField(max_length=500, null=True)
    Educational_Attainment = models.ForeignKey(EducationalAttainment, related_name='+', on_delete=models.CASCADE,
                                               null=True)
    Landline = models.CharField(max_length=500, null=True)
    Mobile_Number = models.CharField(max_length=500, null=True)
    Email = models.CharField(max_length=500, null=True)
    Facebook_Acoount = models.CharField(max_length=500, null=True)

    Fathers_Firstname = models.CharField(max_length=500, null=True)
    Fathers_Middle_Initial = models.CharField(max_length=500, null=True)
    Fathers_Lastname = models.CharField(max_length=500, )
    Educational_Attainment_Father = models.ForeignKey(EducationalAttainment, related_name='+', on_delete=models.CASCADE,
                                                      null=True)
    Father_Occupation = models.CharField(max_length=500, null=True)
    Father_Company_Employed = models.CharField(max_length=500, null=True)
    Father_Landline = models.CharField(max_length=500, null=True)
    Father_MobileNo = models.CharField(max_length=500, null=True)
    Father_Email = models.CharField(max_length=500, null=True)
    Father_Facebook_Account = models.CharField(max_length=500, null=True)

    Mother_FirstName = models.CharField(max_length=500, null=True)
    Mother_Middle_Initial = models.CharField(max_length=500, null=True)
    Mother_Maiden_LastName = models.CharField(max_length=500, null=True)
    Educational_AttainmentID_Mother = models.ForeignKey(EducationalAttainment, related_name='+',
                                                        on_delete=models.CASCADE, null=True)
    Mother_Occupation = models.CharField(max_length=500, null=True)
    Mother_Company_Employed = models.CharField(max_length=500, null=True)
    Mother_Landline = models.CharField(max_length=500, null=True)
    Mother_MobileNo = models.CharField(max_length=500, null=True)
    Mother_Email = models.CharField(max_length=500, null=True)
    Mother_Facebook_Account = models.CharField(max_length=500, null=True)
    Family_Status = models.ForeignKey(FamilyStatu, related_name='+', on_delete=models.CASCADE, null=True)
    Country = models.CharField(max_length=500, null=True)
    ZIP_Postal_Code = models.CharField(max_length=500, null=True)
    State_Province = models.CharField(max_length=500, null=True)
    City = models.CharField(max_length=500, null=True)
    Barangay = models.CharField(max_length=500, null=True)
    Unit_Number_Street = models.CharField(max_length=500, null=True)
    LandMark = models.CharField(max_length=500, null=True)
    AddressLine1 = models.CharField(max_length=500, null=True)
    AddressLine2 = models.CharField(max_length=500, null=True)
    AddressLine3 = models.CharField(max_length=500, null=True)

    def __str__(self):
        suser = '{0.Firstname}   {0.Middle_Initial}      {0.Lastname}'
        return suser.format(self)

class StudentsEnrolledSubject(models.Model):
    Students_Enrollment_Records = models.ForeignKey(StudentsEnrollmentRecord, related_name='+',
                                                    on_delete=models.CASCADE, null=True)
    Subject_Section_Teacher = models.ForeignKey(SubjectSectionTeacher, related_name='+', on_delete=models.CASCADE,
                                                null=True)

这是完整的view.py

this is the full view.py

m = EmployeeUser.objects.get(Username=request.POST['p_user'], Password = request.POST['p_pass'], My_Position =request.POST['position'])
piste = grade.objects.all().filter(Teacher=m.id).values_list('Students_Enrollment_Records').annotate(Average=Avg('Average')).order_by('Grading_Categories').distinct()
return render(request, 'Homepage/index.html', {"piste":piste})

更新

当我尝试@nigel222 先生的这个回答时

when i tried this answer of mr @nigel222

piste = grade.objects.filter(Teacher=m.id).annotate(grade_average=Avg('Average')).order_by('Grading_Categories').distinct()

和我的 html

{% for n in piste %}
      <tr>
          <td>{{n.Teacher}}</td> <!-- 1 -->
          <td>{{n.Subjects}}</td> <!-- 2 -->
          <td>{{n.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}
               </td>  <!-- 3 -->
          <td>{{n}}</td>  <!--4 -->
      </tr>
      {% endfor %}

我得到了这个结果

推荐答案

我已经有几年没有经常使用 Django 了,但我认为这就是正在发生的事情.

I haven't worked with Django much for a couple of years, but this is what I think is happening.

您正在为 piste 分配一个 values_list(一个元组).您没有将 grade 对象分配给 piste.但是,在您的模板中,您希望 piste 的元素是成绩.

You're assigning a values_list (a tuple) to piste. You're not assigning grade objects to piste. However, in your template you're expecting the elements of piste to be grades.

我相信您需要先获取 grade 对象并将其发送到模板以及 piste.

I believe you need to get the grade object first and send that to the template as well as the piste.

这篇关于在 Django 中使用不同的方法时如何使用过滤数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆