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

查看:72
本文介绍了在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)

这是完整视图

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.

您正在将一个values_list(元组)分配给 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天全站免登陆