dimanche 5 avril 2015

Saving Django Form produces error: unorderable types: int() > str()

I have a form which successfully loads existing data from the PostgreSQL database and fills in the fields so that a user can see their own info in an editable form. But when I press the "Save Changes" button (which loads a view which is programmed to save the data back to the same database entry), I get the following error:


(I changed the actual URL here to simply '/PATH/' for ease-of-reading)



TypeError at /accounts/update_profile/

unorderable types: int() > str()

Request Method: POST
Request URL: http://ift.tt/1xSMUJ3
Django Version: 1.7.7
Exception Type: TypeError
Exception Value:

unorderable types: int() > str()

Exception Location: /home/pattmayne/webapps/limbs_006/lib/python3.4/http://ift.tt/1CqQchS in <lambda>, line 279
Python Executable: /usr/local/bin/python3
Python Version: 3.4.1
Python Path:

['/PATH/lib/python3.4/Django-1.7.7-py3.4.egg',
'/PATH',
'/PATH/myproject',
'/PATH/lib/python3.4',
'/usr/local/lib/python34.zip',
'/usr/local/lib/python3.4',
'/usr/local/lib/python3.4/plat-linux',
'/usr/local/lib/python3.4/lib-dynload',
'/usr/local/lib/python3.4/site-packages']

Server time: Sun, 5 Apr 2015 18:46:17 +0000
Traceback Switch to copy-and-paste view

/PATH/lib/python3.4/http://ift.tt/1xSMUJ5 in get_response

response = wrapped_callback(request, *callback_args, **callback_kwargs)

...
▶ Local vars
/PATH/myproject/home/views.py in update_profile

if profile_form.is_valid():

...
▶ Local vars


I think that this error is produced by trying to combine different datatypes. But I haven't explicitly done that. It's being done through some hidden Django process and I can't discover what it is.


Here is the form class in forms.py:



class ProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ('profile_image', 'country', 'description')

def save(self, commit=True):
profile = super(ProfileForm, self).save(commit=True)
profile.profile_image = self.cleaned_data['profile_image']
profile.country = self.cleaned_data['country']
profile.description = self.cleaned_data['description']

if commit:
profile.save()

return profile


And here is the view def which tries to save the form data:



def update_profile(request):
selected_user_id = request.user.id
selected_user_profile = UserProfile.objects.filter(user_id=selected_user_id)[0]
profile_form = ProfileForm(request.POST, instance = selected_user_profile)
if profile_form.is_valid():
profile_form.save()
return HttpResponseRedirect('/accounts/profile')


And here is the original view which successfully loads the data from the DB and displays it (which the user can then alter, and save, to call the previous view)(IMPORTANT: In this scenario only the ELSE stuff gets called):



def user_profile(request):
if request.method == 'GET' and 'profile_id' in request.GET:
profile_id = request.GET.get('profile_id')
selected_user_profile = UserProfile.objects.get(pk=profile_id)
selected_user_id = selected_user_profile.user_id
selected_user = User.objects.get(pk=selected_user_id)
return render(request, 'reg/other_user_profile.html', {
'profile': selected_user_profile, 'selected_user': selected_user
})
else:
selected_user_id = request.user.id
selected_user = request.user
selected_user_profile = UserProfile.objects.filter(user_id=selected_user_id)[0]
profile_form = ProfileForm()
return render(request, 'reg/current_user_profile.html', {
'profile': selected_user_profile, 'selected_user': selected_user, 'profile_form': profile_form
})


Does anybody know where I'm mixing a string with an int? I really have no idea!


1 commentaire: