2
我有一個型號名稱學生:get_list_or_404以多個濾波器在Django
class Student(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
unique_id = models.CharField(max_length=15)
email = models.EmailField(blank=True)
grade = models.ForeignKey(Grade)
hr_teacher = models.ForeignKey(User, related_name='hr_students')
parent = models.ForeignKey(User, related_name='children')
def __unicode__(self):
return u'%s, %s (G-%s, %s)' % (self.last_name, self.first_name, self.grade.short_name, self.unique_id)
def hr_teacher_absent(self):
return self.hr_teacher.groups.filter(name='Absent Teachers').count() > 0
def hr_booking(self):
return self.booking_set.filter(teacher=self.hr_teacher,status__uses_space=True)
def has_booking(self):
return bool(self.hr_booking())
def hr_booking_for_event(self, ptc_event):
return self.hr_booking().filter(period__ptc_event=ptc_event)
def has_booking_for_event(self, ptc_event):
return bool(self.hr_booking_for_event(ptc_event))
def delete_all_bad_bookings(self):
bad_bookings = self.booking_set.exclude(teacher=self.hr_teacher).delete()
class Meta:
ordering = ['grade','last_name','first_name']
每個學生都有一個家房老師這是在組名「教師」用戶(這些都是默認用戶,在Django的定義的組管理員)。我試圖獲得至少有1名學生的小組教師中的用戶名單。這是我的代碼:
teacher_list_db = get_object_or_404(Group, name="Teachers").user_set.annotate(num_students=Count('hr_students')).filter(num_students__gt=0).order_by("last_name","first_name")
它返回一個空數組。
我讀了Django文檔並發現了這種方法:
get_list_or_404(klass, *args, **kwargs)
我試過,結果被困在這裏:
teacher_list_db = get_list_or_404(User.objects.order_by('last_name', 'first_name'), groups__name='Teachers')
我不知道如何使用,其對註釋的部分做學生的數量有相同的老師!
再次測試,上面的解決方案是錯誤的。 –