AttributeError:' NoneType'对象没有属性' all'

时间:2014-09-08 18:29:00

标签: python django django-forms django-templates django-views

我在尝试填写表单时收到此错误。

以下是表单本身:

class locationForm(forms.Form):
    #this form is sent through to give options
    existing_regions= forms.ModelChoiceField(queryset=None, label="Region Name")
    #this form is what is actually is used to determine the associated region (via get_or_create)
    #look at view for details
    region_name = forms.CharField()
    location_name = forms.CharField()
    street_address = forms.CharField()
    city = forms.CharField()
    zip_code = forms.CharField()

我正在使用一个为此表单执行get_or_create的视图:

class AddLocation(View):

    template_name = "dash/Addlocation.html"
    form = locationForm()

    def post(self, request, *args, **kwargs):
        form = locationForm(request.POST)
        user = User.objects.get(username=request.user.username)
        form.fields['existing_regions'].queryset = Region.objects.filter(location__manager=user)
        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 = region_obj,
                name = form.cleaned_data['location_name'],
                street_address = form.cleaned_data['street_address'],
                city = form.cleaned_data['city'],
                zip_code = form.cleaned_data['zip_code']
            )
            location.save()
            own_it = user.get_profile().owned_locations.add(location)

        else:
            form = locationForm(request.POST)
            form.fields['region_name'].queryset = Region.objects.filter(location__manager=user)
            return render(request, self.template_name, {'form': form})

如您所见,'existing_regions'表单提供给前端只是为了提供选项。但最终前端确保将所选名称传递给'region_name',并用于构建保存为Location的对象。不确定这是否是最好的方法,但我不知道我是如何得到这个错误的。如果需要,我可以提供更多细节。提前感谢任何能够阐明这一点的人。

这是完整的追溯

Internal Server Error: /dash/location/add
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/nir/dashboard/pinpoint/apps/locationmanager/views.py", line 64, in post
    return render(request, self.template_name, {'form': form})
  File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py", line 53, in render
    return HttpResponse(loader.render_to_string(*args, **kwargs),
  File "/Library/Python/2.7/site-packages/django/template/loader.py", line 169, in render_to_string
    return t.render(context_instance)
  File "/Library/Python/2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "/Library/Python/2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/Library/Python/2.7/site-packages/django/template/base.py", line 840, in render
    bit = self.render_node(node, context)
  File "/Library/Python/2.7/site-packages/django/template/debug.py", line 78, in render_node
    return node.render(context)
  File "/Library/Python/2.7/site-packages/django/template/debug.py", line 91, in render
    output = force_text(output)
  File "/Library/Python/2.7/site-packages/django/utils/encoding.py", line 100, in force_text
    s = s.__unicode__()
  File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 425, in __str__
    return self.as_widget()
  File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 475, in as_widget
    return widget.render(name, self.value(), attrs=attrs)
  File "/Library/Python/2.7/site-packages/django/forms/widgets.py", line 504, in render
    options = self.render_options(choices, [value])
  File "/Library/Python/2.7/site-packages/django/forms/widgets.py", line 528, in render_options
    for option_value, option_label in chain(self.choices, choices):
  File "/Library/Python/2.7/site-packages/django/forms/models.py", line 1044, in __iter__
    for obj in self.queryset.all():
AttributeError: 'NoneType' object has no attribute 'all'

1 个答案:

答案 0 :(得分:1)

您的原始错误来自于使用ModelChoiceField查询集声明None并且从未在帖子之前分配一个。尝试添加get函数,如下所示:

def get(self, request, *args, **kwargs):
   form = locationForm()
   # Assing proper ModelChoice queryset.
   form.fields['existing_regions'].queryset = Region.objects.filter(
      location__manager=self.request.user)
   # Here put the rest of your get code...

此外,由于您使用的是CBV,您是否考虑过使用FormView而不是普通视图?