65500

Why does my form in Django save successfully except for one field?

Question:

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.

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

models.py

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 nearbyzips?

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 Meta class.

Also, I don't see you calling the save method on your form class in your view function (i.e. form.save()).

Answer2:

Change this line:

tzips = PostalCode.objects.distance(PostalCode.objects.get(code='cleanzipcode').location)

to this:

tzips = PostalCode.objects.distance(PostalCode.objects.get(code=cleanzipcode).location)

Recommend

  • Multiple images in django form with multiupload
  • Get username in url [Django] [mongoengine]
  • how to send a non-english word (chinese) email using django
  • How to redirect people after they register using django registration
  • Django login form in bootstrap popup
  • The symbol you provided is not a function
  • SqlCommand back up Database
  • blade.php method outputting it's result to the form
  • xcode don't localize specific strings
  • How to handle images sent by a mobile device?
  • How do I get HTML corresponding to current DOM tree?
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Django rest serializer Breaks when data exists
  • Meteor helpers not available in Angular template
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Change multiple background-images with jQuery
  • Highlight one bar in a series in highcharts?
  • Ajax Loaded meta Tags
  • Display issues when we change from one jquery mobile page to another in firefox
  • Problems to linebreak with an int in JLabel
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • How to add date and time under each post in guestbook in google app engine
  • Fill an image in a square container while keeping aspect ratio
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Symfony2: How to get request parameter
  • Weird JavaScript statement, what does it mean?
  • jquery mobile loadPage not working
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • Memory offsets in inline assembly
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • JaxB to read class hierarchy
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize