'QuerySet' 对象没有属性“_meta”
'QuerySet' object has no attribute '_meta'
我正在使用 python django 框架开发一个简单的应用程序,我正在使用基于 Class 的视图,当我使用 UpdateView 并尝试 运行 我的模板时,我收到此错误;
'QuerySet' object has no attribute '_meta'
这是我的查看代码
class UpdateStaff(LoginRequiredMixin, UpdateView):
template_name = 'app/update_staff.html'
form_class = UpdateStaffForm
model = Staff
def get_object(self, queryset=None):
obj = Staff.objects.filter(pk=self.kwargs['staff_id'])
return obj
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST or None,
instance=self.get_object())
if form.is_valid():
obj = form.save(commit=False)
obj.save()
messages.success(self.request, "Staff has been updated")
return self.get_success_url()
else:
messages.error(self.request, "Staff not updated")
return HttpResponseRedirect(reverse('update_staff'))
def get_success_url(self):
return HttpResponseRedirect(reverse('manage_staffs'))
def get_context_data(self, **kwargs):
context = super(UpdateStaff,
self).get_context_data()
context['messages'] = messages.get_messages(self.request)
context['form'] = self.form_class(self.request.POST or None,
instance=self.get_object())
return context
这是我的表单代码:
class UpdateStaffForm(ModelForm):
class Meta:
model = Staff
exclude = (
'profile_picture', 'start_work', 'last_login', 'groups',
'user_permissions', 'is_active', 'is_superuser',
'date_joined', 'end_work', 'can_sell_out_of_assigned_area',
'is_staff')
def __init__(self, *args, **kwargs):
super(UpdateStaffForm, self).__init__(*args,
**kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
任何有想法解决这个问题的人请帮助。
get_object
方法 returns queryset
即记录列表,而不是 instance
。要获得 instance
,您可以使用 first()
在 filter()
上。这将使您第一次出现。
def get_object(self, queryset=None):
obj = Staff.objects.filter(pk=self.kwargs['staff_id']).first()
return obj
迟到的答案,但值得。
from django.shortcuts import get_object_or_404
...
def get_object(self, queryset=None):
obj = get_object_or_404(Staff, pk=self.kwargs['staff_id'])
return obj
如果找不到对象,此方法将 return HTTP 404 Not Found
响应。
我正在使用 python django 框架开发一个简单的应用程序,我正在使用基于 Class 的视图,当我使用 UpdateView 并尝试 运行 我的模板时,我收到此错误;
'QuerySet' object has no attribute '_meta'
这是我的查看代码
class UpdateStaff(LoginRequiredMixin, UpdateView):
template_name = 'app/update_staff.html'
form_class = UpdateStaffForm
model = Staff
def get_object(self, queryset=None):
obj = Staff.objects.filter(pk=self.kwargs['staff_id'])
return obj
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST or None,
instance=self.get_object())
if form.is_valid():
obj = form.save(commit=False)
obj.save()
messages.success(self.request, "Staff has been updated")
return self.get_success_url()
else:
messages.error(self.request, "Staff not updated")
return HttpResponseRedirect(reverse('update_staff'))
def get_success_url(self):
return HttpResponseRedirect(reverse('manage_staffs'))
def get_context_data(self, **kwargs):
context = super(UpdateStaff,
self).get_context_data()
context['messages'] = messages.get_messages(self.request)
context['form'] = self.form_class(self.request.POST or None,
instance=self.get_object())
return context
这是我的表单代码:
class UpdateStaffForm(ModelForm):
class Meta:
model = Staff
exclude = (
'profile_picture', 'start_work', 'last_login', 'groups',
'user_permissions', 'is_active', 'is_superuser',
'date_joined', 'end_work', 'can_sell_out_of_assigned_area',
'is_staff')
def __init__(self, *args, **kwargs):
super(UpdateStaffForm, self).__init__(*args,
**kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
任何有想法解决这个问题的人请帮助。
get_object
方法 returns queryset
即记录列表,而不是 instance
。要获得 instance
,您可以使用 first()
在 filter()
上。这将使您第一次出现。
def get_object(self, queryset=None):
obj = Staff.objects.filter(pk=self.kwargs['staff_id']).first()
return obj
迟到的答案,但值得。
from django.shortcuts import get_object_or_404
...
def get_object(self, queryset=None):
obj = get_object_or_404(Staff, pk=self.kwargs['staff_id'])
return obj
如果找不到对象,此方法将 return HTTP 404 Not Found
响应。