my form in forms.py is then passed to this method in my views.py, if I go into python shell and print objects from MyProfile, all of the fields show values except for nearbyzips, which shows None. As you can see below, I am trying to manually assign a value to nearbyzips when the form is saved.
@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() cleanzipcode = form.cleaned_data['zipcode'] nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code=cleanzipcode).location) zip_codes = list(nearestzips.values_list('code', flat=True)) //print zip_codes form.cleaned_data['nearbyzips'] = zip_codes //print form.cleaned_data['nearbyzips'] profile=form.save() return redirect(redirect_to)
class MyProfile(UserenaBaseProfile): user = models.OneToOneField(User, unique=True, verbose_name=_('user'), related_name='my_profile') streetaddress=models.CharField(null=True, blank=True, max_length=30) city = models.CharField(null=True, blank=True, max_length=20) state = models.CharField(null=True, blank=True, max_length=20) zipcode = models.IntegerField(_('zipcode'), max_length=5, null=True, blank=True) nearbyzips = models.IntegerField(null=True, blank=True, max_length=100) phone=models.CharField(null=True, blank=True, max_length=16) websiteurl=models.CharField(null=True, blank=True, max_length=38)
Something to keep in mind, if I go into python shell and run:
nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code='97202').location print nearestzips
It prints all the Postal Codes I would expect. So I'm not sure where exactly is broken. I don't see any errors in my logs.
UPDATE: I have added print statements in my views. printing zip_codes and form.cleaned_data['nearbyzips'] both show:
[u'97202', u'97206', u'97214', u'97215', u'97239']
But it still does not appear to be saving to the form.Answer1:
2 things stand out to me here.
Your form is created for some kind of profile model (get_profile_model()) -- does this profile model have a field called
If your model does have a field called
nearbyzips, explicitly include it (and all the fields you want to update) in a <a href="https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/" rel="nofollow">tuple/list of
fields</a> in your form class's inner
Also, I don't see you calling the
save method on your form class in your view function (i.e.
Change this line:
tzips = PostalCode.objects.distance(PostalCode.objects.get(code='cleanzipcode').location)
tzips = PostalCode.objects.distance(PostalCode.objects.get(code=cleanzipcode).location)