Code

Opened 4 years ago

Closed 4 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: UI/UX:

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.

Attachments (0)

Change History (4)

comment:1 follow-up: Changed 4 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

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

comment:2 in reply to: ↑ 1 Changed 4 years ago by petrikuittinen@…

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 Changed 4 years ago by anonymous

  • Has patch set
  • Needs tests set
  • Resolution duplicate deleted
  • Status changed from closed to reopened

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 Changed 4 years ago by kmtracey

  • Resolution set to duplicate
  • Status changed from reopened to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.