Python 非类型';对象没有属性';型号';
我的一张表格给了我这个错误,而我似乎无法找出问题所在Python 非类型';对象没有属性';型号';,python,django,django-models,django-forms,django-views,Python,Django,Django Models,Django Forms,Django Views,我的一张表格给了我这个错误,而我似乎无法找出问题所在 11:24:04 web.1 | Internal Server Error: /dash/location/add 11:24:04 web.1 | Traceback (most recent call last): 11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 114, in get_re
11:24:04 web.1 | Internal Server Error: /dash/location/add
11:24:04 web.1 | Traceback (most recent call last):
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
11:24:04 web.1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
11:24:04 web.1 | return view_func(request, *args, **kwargs)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/views/generic/base.py", line 69, in view
11:24:04 web.1 | return self.dispatch(request, *args, **kwargs)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
11:24:04 web.1 | return handler(request, *args, **kwargs)
11:24:04 web.1 | File "/Users/nir/dashboard/pinpoint/apps/locationmanager/views.py", line 43, in post
11:24:04 web.1 | if form.is_valid():
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 129, in is_valid
11:24:04 web.1 | return self.is_bound and not bool(self.errors)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 121, in errors
11:24:04 web.1 | self.full_clean()
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 273, in full_clean
11:24:04 web.1 | self._clean_fields()
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 288, in _clean_fields
11:24:04 web.1 | value = field.clean(value)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/fields.py", line 148, in clean
11:24:04 web.1 | value = self.to_python(value)
11:24:04 web.1 | File "/Library/Python/2.7/site-packages/django/forms/models.py", line 1136, in to_python
11:24:04 web.1 | except (ValueError, self.queryset.model.DoesNotExist):
11:24:04 web.1 | AttributeError: 'NoneType' object has no attribute 'model'
这是发生错误的视图:
class AddLocation(View):
template_name = "dash/Addlocation.html"
form = locationForm()
def get(self, request, *args, **kwargs):
user = User.objects.get(username=request.user.username)
self.form.fields['region_name'].queryset = Region.objects.filter(location__manager=user)
return render(request, self.template_name, {'form': self.form})
def post(self, request, *args, **kwargs):
form = locationForm(request.POST)
if form.is_valid():
region_obj, _created = Region.objects.get_or_create(name=form.cleaned_data['region_name'])
form.cleaned_data['region_name'] = region_obj
user = User.objects.get(username=request.user.username)
location = Location(
region_obj,
user,name = form.cleaned_data['name'],
street_address = form.cleaned_data['street_address'],
city = form.cleaned_data['city'],
zip_code = form.cleaned_data['zip_code']
)
location.save()
return redirect(reverse('location_manager'))
else:
form = locationForm(request.POST)
return render(request, self.template_name, {'form': form})
具体来说,在尝试验证表单是否有效()时,似乎出现了问题:
如果需要,我可以发布更多信息,但我真的不知道这个错误是什么意思。有人能解释一下吗
编辑:
这是表格本身
from django import forms
class locationForm(forms.Form):
region_name = forms.ModelChoiceField(queryset=None, label="Region Name")
location_name = forms.CharField()
street_address = forms.CharField()
city = forms.CharField()
zip_code = forms.CharField()
您已将区域名称的查询集显式定义为“无”。这是无效的:您需要一个查询集。您确实在视图的
get
方法中正确地分配了查询集,但在post中没有,因此在post上验证表单时,当然会失败
与其在视图中这样做,不如在调用
super
locationForm之后,真正重写表单的\uuuu init\uuuu
方法并在那里分配查询集。locationForm看起来像什么?这里:user,name=form.cleaned\u data['name',
您只定义了name
,而不是user
,您的post()
方法缺少ELSE
。它只有在时才有一个。如果if-valid语句失败了怎么办?你真的应该为表单本身提供代码。我已经为Aron Ysidoro添加了基于类的视图的完整代码,你可以看到else在那里,我只提供了一个片段。Zclancy&Daniel Roseman,我已经提供了表格本身。任何帮助都将不胜感激。嗨!几年后,我得到了这个确切的消息,但我正在表单的init方法中设置queryset,正如您所说的那样。另外,表单将按照预期在模板中呈现,并使用queryset中的正确选项。只有在提交表单时才会出现此错误。本地变量也给出了我期望的值。真的不知道是什么触发了这条信息