gpt4 book ai didi

python - NoneType' 对象没有属性 'model'

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:12 24 4
gpt4 key购买 nike

我的一个表单出现此错误,我似乎无法弄清楚问题出在哪里。

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'

这是错误发生的 View :

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})

具体来说,问题似乎是在尝试验证 if form.is_valid():

时出现的

如果需要,我可以发布更多信息,但我真的不知道这个错误是什么意思。有人可以阐明这一点吗?

编辑:这是表格本身

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()

最佳答案

您已明确将 region_name 的查询集定义为 None。那是无效的:您需要一个查询集。您确实在 View 的 get 方法中正确分配了一个查询集,但在 post 中却没有,因此当涉及到在 post 上验证表单时,它当然会失败。

与其在 View 中执行此操作,不如在调用 super 之后重写表单的 __init__ 方法并在那里分配查询集。

关于python - NoneType' 对象没有属性 'model',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25293706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com