简体   繁体   中英

NoneType' object has no attribute 'model'

One of my forms is giving me this error, and I cannot seem to figure out what the problem is.

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'

This is the view that the error is occuring in:

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

specifically it seems that the problem is occurring when trying to validate if form.is_valid():

I can post more information if needed, but I really have no clue what this error is getting at. Can someone shed some light on this?

Edit: Here is the form itself

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

You have explicitly defined the queryset for the region_name to be None. That's not valid: you need a queryset. You do assign a queryset correctly in the get method of your view, but not in the post, so of course when it comes to validate the form on post, it fails.

Rather than doing that in the view, you should really override the __init__ method of the form and assign the queryset there, after calling super .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM