diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 6a2d982351..e2cd2113f8 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -146,7 +146,7 @@ class ForeignKeyRawIdWidget(forms.TextInput): def label_for_value(self, value): key = self.rel.get_related_field().name - obj = self.rel.to.objects.get(**{key: value}) + obj = self.rel.to._default_manager.get(**{key: value}) return ' %s' % truncate_words(obj, 14) class ManyToManyRawIdWidget(ForeignKeyRawIdWidget): diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py index 19e2696b93..ddfc6c2cb6 100644 --- a/tests/regressiontests/admin_widgets/models.py +++ b/tests/regressiontests/admin_widgets/models.py @@ -24,10 +24,19 @@ class Album(models.Model): def __unicode__(self): return self.name +class HiddenInventoryManager(models.Manager): + def get_query_set(self): + return super(HiddenInventoryManager, self).get_query_set().filter(hidden=False) + class Inventory(models.Model): barcode = models.PositiveIntegerField(unique=True) parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True) name = models.CharField(blank=False, max_length=20) + hidden = models.BooleanField(default=False) + + # see #9258 + default_manager = models.Manager() + objects = HiddenInventoryManager() def __unicode__(self): return self.name @@ -101,6 +110,12 @@ True >>> w = ForeignKeyRawIdWidget(rel) >>> print w.render('test', core.parent_id, attrs={}) Lookup Apple + +# see #9258 +>>> hidden = Inventory.objects.create(barcode=93, name='Hidden', hidden=True) +>>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden) +>>> print w.render('test', child_of_hidden.parent_id, attrs={}) + Lookup Hidden """ % { 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX, 'STORAGE_URL': default_storage.url(''),