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

雨燕双飞 提交于 2020-01-06 07:25:34

问题


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.


回答1:


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 tuple/list of fields 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()).




回答2:


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)


来源:https://stackoverflow.com/questions/28549474/why-does-my-form-in-django-save-successfully-except-for-one-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!