django 自定义中不存在相关对象用户没有学员

eqzww0vc  于 2023-02-20  发布在  Go
关注(0)|答案(1)|浏览(141)

我正在尝试根据登录的行业主管筛选日志报告,主管应该能够看到其监督下的学生的报告
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
ru9i0ody

ru9i0ody1#

可以在LogbookEntryView类中更新get_queryset方法

def get_queryset(self):
        user = self.request.user
    
        # If user is not authenticated, return empty queryset
        if not user.is_authenticated:
            return LogbookEntry.objects.none()
    
        # If user is not a student, return empty queryset
        if not hasattr(user, 'student'):
            return LogbookEntry.objects.none()
    
        # Get the industry supervisor for the student
        supervisor = user.student.industry_based_supervisor
    
        # If student does not have an industry supervisor, return empty queryset
        if supervisor is None:
            return LogbookEntry.objects.none()
    
        # Filter logbook entries based on the industry supervisor
        queryset = LogbookEntry.objects.filter(student__industrysupervisor=supervisor)
    
        return queryset

相关问题