Reputation: 9615
I am getting this error when attempting to fill out a form.
Here is the form itself:
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()
And I am using a view that does a get_or_create for this form:
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})
As you can see, the 'existing_regions' form is provided to the front end just to provide the options. But ultimately front end makes sure to pass the name selected to 'region_name' and that is used to build the object that is saved as a Location. Not sure if this is the best way to do it, but I don't know how I got this error. I can provide more details if needed. Thanks in advance to anyone who can shed light on this.
Here is the full traceback
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'
Upvotes: 0
Views: 3254
Reputation: 171
Your original error comes from declaring the ModelChoiceField
with a None
queryset and never assigning one before the post. Try adding a get
function, something like this:
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...
Also, since you are using a CBV, have you considered using a FormView instead of the plain View?
Upvotes: 1