The NoneType object has no attribute 'model'

One of my forms is giving me this error and I cannot 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 an idea of ​​what the error is happening 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})

      

in particular, it seems that the problem occurs when trying to check if form.is_valid():

I can post more information if needed, but I really don't know what's going on with this error. 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()

      

+3


source to share


1 answer


You have explicitly defined the query for region_name as None. This is not valid: you need a request. You are assigning the request correctly in the method of get

your view, but not in the post, so of course, when it comes to validating the form in the post, this fails.



Instead of doing it in the view, you should really override the __init__

form method and assign a set of requests there after the call super

.

+6


source







All Articles