"NoneType"对象没有属性"model"。

5
我的一个表单出现了这个错误,但是我似乎无法找出问题所在。
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})

具体来说,当尝试验证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()

locationForm长什么样子? - Zac Clancy
在这里:user, name = form.cleaned_data['name'], 你只定义了 name 而没有定义 user - Aaron Lelevier
另外,你的 post() 方法缺少 ELSE。它只有一个 IF。如果 IF 语句无效怎么办? - Aaron Lelevier
你真的应该提供表单本身的代码。 - Daniel Roseman
我已经添加了基于类的视图Aron Ysidoro的完整代码,您可以看到else在那里,我只提供了一小部分。Zclancy和Daniel Roseman,我已经提供了表单本身。任何帮助将不胜感激。 - ApathyBear
1个回答

8
您已经明确地将region_name的queryset定义为None。这是无效的:您需要一个queryset。您在视图的get方法中正确地分配了queryset,但在post中没有这样做,所以当在post上验证表单时,它会失败。
相比于在视图中执行此操作,您应该真正重写表单的__init__方法,并在调用super后在那里分配queryset。

你好!多年以后,我遇到了完全相同的问题,但是我已经按照你所说的在表单的init方法中设置了queryset。此外,表单在模板中呈现为预期,具有来自queryset的正确选项。当提交表单时,才会出现此错误。本地变量也显示了我预期的值。真的不知道是什么触发了这个消息。 - fdeboo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接