Opened 14 years ago

Closed 14 years ago

#13606 closed (duplicate)

admin raw_id_fields fail to check against non-numerical input

Reported by: petrikuittinen@… Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Inputting a non-numerical value in a foreign key field using raw_input_fields produces a ValueError exception in django admin.
E.g write "wer" to any foreign key field, which has been declared with raw_input_fields fails:

Django Version:	1.2.1
Exception Type:	ValueError
Exception Value:	
invalid literal for int() with base 10: 'wer'

Using Django 1.2-beta I was able to fix this bug by simply appending these two lines to
django/contrib/admin/widgets.py label_for_value()-function:


def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try: 
            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
        except self.rel.to.DoesNotExist: 
            return ''
        # simple fix  
        except ValueError:
            return ''
        # end of fix
        return '&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))

Now in Django 1.2.1 this fix unfortunately doesn't work.

Change History (4)

comment:1 by Karen Tracey, 14 years ago

Resolution: duplicate
Status: newclosed

Isn't this #13149? We only need one ticket to track getting it fixed.

in reply to:  1 comment:2 by petrikuittinen@…, 14 years ago

Replying to kmtracey:

Isn't this #13149? We only need one ticket to track getting it fixed.

Yes. It is the same issue indeed. Unfortunately the patch in #13149 doesn't solve the problem in Django-1.2.1. I don't know why. The patch works for Django-1.2-beta though.

comment:3 by anonymous, 14 years ago

Has patch: set
Needs tests: set
Resolution: duplicate
Status: closedreopened

I know this a duplicate ticket, BUT the previous ticket for was Django version 1.1.1.
Django 1.2+ needs to have patches in two files to get this issue fixed.

changes to django/forms/models.py from line 984:

    def to_python(self, value):
        if value in EMPTY_VALUES:
            return None
        try:
            key = self.to_field_name or 'pk'
            value = self.queryset.get(**{key: value})
        except (ValueError, self.queryset.model.DoesNotExist):
            raise ValidationError(self.error_messages['invalid_choice'])
        return value

And django/contrib/admin/widgets.py

    def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
            return '&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))
        except (ValueError, self.rel.to.DoesNotExist):
            return ''

Applying both of these patches seems to fix the issue. It would be nice to have this fixed in the next Django 1.2.2 release.

comment:4 by Karen Tracey, 14 years ago

Resolution: duplicate
Status: reopenedclosed

The previous ticket (#13149) is still open; the place to post information on what is necessary to get it fixed in current trunk code is there. It's one problem, there should be only one ticket for tracking getting it fixed. The version for that ticket being set to 1.1 just indicates that the problem has been around at least since 1.1, it does not mean the fix will be specific to 1.1. For any ticket the fix will be made to current trunk and backported to the current release branch (if necessary). If the existing patches on that ticket no longer work, then it should be marked patch needs improvement, or better yet a new patch should be uploaded that fixes the problem for current code. A second ticket to track the same problem is not helpful.

Note: See TracTickets for help on using tickets.
Back to Top