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

前端 未结 3 1940
后悔当初
后悔当初 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条回答
  •  猫巷女王i
    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 %}
          
              {{n.Teacher}} 
              {{n.Subjects}} 
              {{n.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}
                     
              {{n}}  
          
          {% 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)
    

提交回复
热议问题