Reputation: 2393
I get the following error:
DoesNotExist: PostalCode matching query does not exist
on the line below:
nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code='self.zipcode').location)
In an effort to troubleshoot the issue. I went into my python shell and was able to run:
PostalCode.objects.distance(PostalCode.objects.get(code='97202').location)
Which returned the proper zipcodes. I think what this means is that I am inappropriately using self.zipcode in the original query? I thought I should be using self.zipcode because basically the view is for an edit_profile
form which has a zipcode
field. So I thought self.zipcode
would be taking the users zipcode from that form and then running the query with that value. What am I doing wrong here?
Here is more of the code if it helps:
views.py
@secure_required
@login_required
def profile_edit(request, username, edit_profile_form=EditProfileForm,
template_name='userena/profile_form.html', success_url=None,
extra_context=None, **kwargs):
profile = get_profile(user)
form = edit_profile_form(instance=profile, initial=user_initial)
if request.method == 'POST':
if form.is_valid()
nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code='self.zipcode').location)
zip_codes = list(nearestzips.values_list('code', flat=True))
user.nearbyzips = zip_codes
return redirect(redirect_to)
forms.py
class EditProfileForm(forms.ModelForm):
""" Base form used for fields that are always required """
first_name = forms.CharField(label=_(u'First name'),
max_length=30,
required=False)
last_name = forms.CharField(label=_(u'Last name'),
max_length=30,
required=False)
def __init__(self, *args, **kw):
super(forms.ModelForm, self).__init__(*args, **kw)
# Put the first and last name at the top
new_order = self.fields.keyOrder[:-2]
new_order.insert(0, 'first_name')
new_order.insert(1, 'last_name')
self.fields.keyOrder = new_order
class Meta:
model = get_profile_model()
exclude = ['user']
def save(self, force_insert=False, force_update=False, commit=True):
profile = super(EditProfileForm, self).save(commit=commit)
# Save first and last name
user = profile.user
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
return profile
Upvotes: 1
Views: 207
Reputation: 22697
remove ''
from self.zipcode
. Also you said that you want to get zipcode
from a form
, so,self
is not defined, you need to get zipcode from form
variable
if request.method == 'POST':
if form.is_valid()
zipcode = form.cleaned_data['zipcode']
nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code=zipcode).location)
Upvotes: 1