Ticket #13149: ForeignKeyRawIdWidget-invalid-value.patch

File ForeignKeyRawIdWidget-invalid-value.patch, 1021 bytes (added by acdha, 5 years ago)

Patch against Django 1.1.1

  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index fb5acb5..d9f2e0b 100644
    a b class ForeignKeyRawIdWidget(forms.TextInput): 
    148148
    149149    def label_for_value(self, value):
    150150        key = self.rel.get_related_field().name
    151         obj = self.rel.to._default_manager.get(**{key: value})
    152         return '&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))
     151
     152        # Our value will not be valid if the user enters incorrect data (i.e.
     153        # non-existent pk or a non-integer value). Normal form validation will
     154        # handle this as long as we don't raise an exception here:
     155        try:
     156            obj = self.rel.to._default_manager.get(**{key: value})
     157            return '&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))
     158        except (ValueError, self.rel.to.DoesNotExist):
     159            return ""
    153160
    154161class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
    155162    """
Back to Top