我遇到错误。
在/ timesheet / report /引用了本地变量'qs'的UnboundLocalError 分配前
class ReportView(LoginRequiredMixin, generic.ListView):
template_name = 'report/report.html'
paginate_by = constants.PAGINATED_NUMBER
context_object_name = 'reports'
def get_queryset(self, *args, **kwargs):
try:
timesheet_latest_ids = TimesheetEntry.objects.raw('SELECT MAX(id) as id FROM timesheet_entry WHERE timesheet_is_running = 0 GROUP BY timesheet_users_id ORDER BY timesheet_clock_out_on DESC')
list1 = []
for timesheet_latest_id in timesheet_latest_ids:
list1.append(timesheet_latest_id.id)
result = TimesheetEntry.objects.filter(id__in=list1)
if self.request.user.userprofile.user_role.id == 2 : # 2 for admin
qs = TimesheetEntry.objects.filter(
id__in=list1
).filter(
timesheet_users__userprofile__user_company=self.request.user.userprofile.user_company
)
if self.request.user.userprofile.user_role.id == 3 : # 3 for manager
qs = TimesheetEntry.objects.filter(
id__in=list1
).filter(
timesheet_users__userprofile__user_company=self.request.user.userprofile.user_company
).exclude(timesheet_users__userprofile__user_role_id=3) | TimesheetEntry.objects.filter(timesheet_users__pk=self.request.user.pk)
if self.request.user.userprofile.user_role.id == 4 : # 4 for employee
qs = TimesheetEntry.objects.filter(timesheet_users=self.request.user.id).latest('id')
except TimesheetEntry.DoesNotExist:
qs=None
return qs
如果我将qs=None
放在except
中,就像这样
except TimesheetEntry.DoesNotExist:
qs = None
然后我遇到这样的错误
在/ timesheet / report /类型为'NoneType'的对象上的TypeError没有len()
我如何不退回
答案 0 :(得分:1)
您可以像以下link一样传递None查询集
except TimesheetEntry.DoesNotExist:
qs = TimesheetEntry.objects.none()