Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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://www.limbs.ca/accounts/update_profile/
Django Version:     1.7.7
Exception Type:     TypeError
Exception Value:    

unorderable types: int() > str()

Exception Location:     /home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/validators.py 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/Django-1.7.7-py3.4.egg/django/core/handlers/base.py 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!

EDIT: Here is the "copy and paste" traceback: (plus a live link to it): http://dpaste.com/04YYD16

Environment:


Request Method: POST
Request URL: http://www.limbs.ca/accounts/update_profile/

Django Version: 1.7.7
Python Version: 3.4.1
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'testapp',
 'home')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/pattmayne/webapps/limbs_006/myproject/home/views.py" in update_profile
  129.     if profile_form.is_valid():
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/forms/forms.py" in is_valid
  162.         return self.is_bound and not bool(self.errors)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/forms/forms.py" in errors
  154.             self.full_clean()
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/forms/forms.py" in full_clean
  355.         self._post_clean()
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/forms/models.py" in _post_clean
  422.             self.instance.full_clean(exclude=exclude, validate_unique=False)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/models/base.py" in full_clean
  990.             self.clean_fields(exclude=exclude)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/models/base.py" in clean_fields
  1032.                 setattr(self, f.attname, f.clean(raw_value, self))
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/models/fields/__init__.py" in clean
  512.         self.run_validators(value)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/models/fields/__init__.py" in run_validators
  464.                 v(value)
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/validators.py" in __call__
  245.         if self.compare(cleaned, self.limit_value):
File "/home/pattmayne/webapps/limbs_006/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/validators.py" in <lambda>
  279.     compare = lambda self, a, b: a > b

Exception Type: TypeError at /accounts/update_profile/
Exception Value: unorderable types: int() > str()

EDIT #2: Models definitions:

class UserProfile(models.Model):
    country = models.CharField(default="Earth", max_length="50")
    description = models.TextField(default="One more happy user...")
    friends = models.ManyToManyField("self", blank=True, null=True)
    profile_image = models.FileField(upload_to='prof_img/user/%Y/%m/%d', default='prof_img/user/default.jpg')
    user = models.OneToOneField(User)
    def __str__(self):
        return user.username
like image 686
Matt Payne Avatar asked Dec 19 '22 05:12

Matt Payne


1 Answers

Looks like the error is in your definition of the country field: you've put max_length="50" instead of max_length=50.

The thing that's being compared is the length of the value, an int, with what you've defined as the max length, a string.

like image 174
Daniel Roseman Avatar answered Dec 22 '22 01:12

Daniel Roseman