How to use filtering data while using distinct method in django?

前端 未结 3 1925
后悔当初
后悔当初 2020-12-22 10:05

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

When I tried this:

id_list = g         


        
相关标签:
3条回答
  • 2020-12-22 10:06

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

    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.

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

    0 讨论(0)
  • 2020-12-22 10:28

    This is the best I can do for you. I think you should be selected based on the student and filtering the grades to the teacher. I also did a prefetch for all of the subjects since I can't quite tell what you need done there.

    students = Student.objects.filter(
        grades__teacher_id=teacher.id,
    ).annotate(
        total_avg=Avg('grades__Average')
    ).prefetch_related('grades__Subjects')
    

    Template:

    {% for student in students %}
          <tr>
              <td>{{teacher}}</td>
              <td>{{student.grades.subjects.all}}</td>
              <td>{{student}}</td>
              <td>{{student.total_avg}}</td>
          </tr>
    {% endfor %}
    
    0 讨论(0)
  • 2020-12-22 10:31

    You don't want values_list (which is the data you are getting in column 4). You want a queryset of grade objects:

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

    and then in your template, something like

    {% 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 %}
    

    1 and 2 render EmployeeUser and Subject objects. 1 will return the __str__ representation of EmployeeUser which ought to be OK. Alternatively you can explicitly use {{n.Teacher.FirstName}} etc. in your template.

    3 I don't understand, because you don't show the Students_Enrolled_Subject model.

    4 is now wrong. Perhaps you want the annotation {{n.Average}}?

    Please, as soon as possible, learn to use Django/ Python coding conventions. Model subclasses (and Python class names in general) start with a capital letter. Instances of classes are lowercase. Fieldnames/attributes are normally lower_case and definitely start with a lowercase letter. Model subclass names are a singular name not a plural name. Not doing this is horribly confusing to any experiemced Django coder. So,

    class Grade(models.Model):
        teacher = models.ForeignKey(EmployeeUser, related_name='+', on_delete=models.CASCADE,
                                null=True, blank=True)
        grading_categories = models.ForeignKey(GradingCategory, related_name='+', on_delete=models.CASCADE,
                                           null=True, blank=True)
        subject = 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)
    
    0 讨论(0)
提交回复
热议问题