Ticket #11607: 11607.diff

File 11607.diff, 2.8 KB (added by Peter Baumgartner, 15 years ago)
  • django/contrib/admin/widgets.py

     
    147147
    148148    def label_for_value(self, value):
    149149        key = self.rel.get_related_field().name
    150         obj = self.rel.to._default_manager.get(**{key: value})
    151         return '&nbsp;<strong>%s</strong>' % truncate_words(obj, 14)
     150        try:
     151            obj = self.rel.to._default_manager.get(**{key: value})
     152            label = '&nbsp;<strong>%s</strong>' % truncate_words(obj, 14)
     153        except self.rel.to.DoesNotExist:
     154            label = ''
     155        return label
    152156
    153157class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
    154158    """
  • tests/regressiontests/admin_widgets/urls_rawid.py

     
     1
     2from django.conf.urls.defaults import *
     3import rawidadmin
     4
     5urlpatterns = patterns('',
     6    (r'^rawid_admin/', include(rawidadmin.site.urls)),
     7)
  • tests/regressiontests/admin_widgets/rawidadmin.py

     
     1"""
     2
     3"""
     4from django.contrib import admin
     5
     6import models
     7
     8class RawIdAdmin(admin.AdminSite):
     9    pass
     10
     11class CarAdmin(admin.ModelAdmin):
     12    raw_id_fields = ['owner']
     13
     14site = RawIdAdmin(name='rawid-admin')
     15
     16site.register(models.User)
     17site.register(models.Car, CarAdmin)
  • tests/regressiontests/admin_widgets/tests.py

     
    130130class OldAdminForeignKeyWidgetChangeList(AdminForeignKeyWidgetChangeList):
    131131    urls = 'regressiontests.admin_widgets.urls2'
    132132    admin_root = '/deep/down/admin'
     133   
     134class AdminForeignKeyRawIdWidgetInvalidId(DjangoTestCase):
     135    urls = 'regressiontests.admin_widgets.urls_rawid'
     136    fixtures = ["admin-widgets-users.xml"]
     137    admin_root = '/rawid_admin'
     138
     139    def setUp(self):
     140        self.client.login(username="super", password="secret")
     141
     142    def tearDown(self):
     143        self.client.logout()
     144
     145    def test_invalid_id(self):
     146        response = self.client.post('%s/admin_widgets/car/add/' % self.admin_root,
     147                                    {'owner': '999', # does not exist
     148                                     'make': 'VW',
     149                                     'model': 'Karmann Ghia'})
     150        self.assertEqual(response.status_code, 200)
Back to Top