在这个基于类的视图....假设queryset = self.objects.get(user_assigned=pk)
有一个值…我想让它把它还给AccountModify
,这样def get_object(self, queryset=None):
就可以拿走它并把它还给它。现在def get_object()
没有收到它,返回空白queryset
class AccountCreateOrModify(object):
model = Employee
form_class = AccountForm
template_name = 'bot_data/account_modify.html'
success_url = reverse_lazy('home')
class AccountModify(LoginRequiredMixin,
AccountCreateOrModify,
UpdateView):
def dispatch(self, request,
*args, **kwargs):
try:
pk = self.request.user.pk
queryset = self.model.objects.get(user_assigned=pk)
except Employee.DoesNotExist:
return redirect('account_add')
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
return handler(request, queryset)
def get_object(self, queryset=None):
return queryset
在我看来,你应该把查询(...objects.get(user_assigned=pk)
)在get_object
方法内。
要重定向到account_add
,可以这样包装调度方法:
def dispatch(self, request, *args, **kwargs):
try:
return super(AccountModify, self).dispatch(request, *args, **kwargs)
except Employee.DoesNotExist:
return redirect('account_add')