我正在尝试根据登录的行业主管筛选日志报告,主管应该能够看到其监督下的学生的报告
views.py
class LogbookEntryView(ListAPIView):
queryset = LogbookEntry.objects.all()
serializer_class = StudentLogbookEntrySerializer
def get_queryset(self, *args, **kwargs):
qs = super().get_queryset(*args, **kwargs)
request = self.request
user = request.user
if not user.is_authenticated:
LogbookEntry.objects.none()
return qs.filter(student_id__industrysupervisor = request.user.student.industry_based_supervisor)
models.py 簿条目模型
class LogbookEntry(models.Model):
week = models.ForeignKey("api.WeekDates", verbose_name=_("Week Id"), null=True, on_delete=models.SET_NULL)
student = models.ForeignKey("students.Student", verbose_name=_("Student Id"), on_delete=models.CASCADE)
entry_date = models.DateTimeField()
title = models.CharField(_("Title"), max_length=50)
description = models.CharField(_("Description"), max_length=1000)
diagram = models.ImageField(_("Diagram"), upload_to=profile_picture_dir)
学生模型
class Student(models.Model):
user = models.OneToOneField(get_user_model(), null=True, on_delete=models.CASCADE)
profile_pic = models.ImageField(_("profile picture"), upload_to=profile_picture_dir)
department_id = models.ForeignKey(Department, null=True, on_delete=models.SET_NULL)
phone_no = models.CharField(max_length=11)
school_based_supervisor = models.ForeignKey("school_based_supervisor.SchoolSupervisor", verbose_name=_("School Supervisor"), null=True, on_delete=models.SET_NULL)
industry_based_supervisor = models.ForeignKey("industry_based_supervisor.IndustrySupervisor", verbose_name=_("Industry Supervisor"), null=True, on_delete=models.SET_NULL)
placement_location = models.ForeignKey("industry_based_supervisor.PlacementCentre", verbose_name=_("Placement Location"), null=True, blank=True, on_delete=models.SET_NULL)
行业主管模型
class IndustrySupervisor(models.Model):
user = models.OneToOneField(get_user_model(), null=True, on_delete=models.CASCADE)
profile_pic = models.ImageField(_("profile picture"), upload_to=profile_picture_dir)
phone_no = models.CharField(max_length=11)
placement_center = models.ForeignKey("industry_based_supervisor.PlacementCentre", verbose_name=_("Placement Centre"), null=True, blank=True, on_delete=models.CASCADE)
def __str__(self):
return self.user.username
1条答案
按热度按时间ru9i0ody1#
可以在
LogbookEntryView
类中更新get_queryset
方法