Ticket #9258: foreing-key-raw-id-admin.diff

File foreing-key-raw-id-admin.diff, 3.2 KB (added by nullie, 7 years ago)
  • django/contrib/admin/widgets.py

     
    146146           
    147147    def label_for_value(self, value):
    148148        key = self.rel.get_related_field().name
    149         obj = self.rel.to.objects.get(**{key: value})
     149        obj = self.rel.to._default_manager.get(**{key: value})
    150150        return '&nbsp;<strong>%s</strong>' % truncate_words(obj, 14)
    151151
    152152class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
  • tests/regressiontests/admin_widgets/models.py

     
    2424    def __unicode__(self):
    2525        return self.name
    2626
     27class HiddenInventoryManager(models.Manager):
     28    def get_query_set(self):
     29        return super(HiddenInventoryManager, self).get_query_set().filter(hidden=False)
     30
    2731class Inventory(models.Model):
    28    barcode = models.PositiveIntegerField(unique=True)
    29    parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True)
    30    name = models.CharField(blank=False, max_length=20)
     32    hidden = models.BooleanField(default=False)
     33    barcode = models.PositiveIntegerField(unique=True)
     34    parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True)
     35    name = models.CharField(blank=False, max_length=20)
    3136
    32    def __unicode__(self):
    33       return self.name
     37    default_manager = models.Manager()
     38    objects = HiddenInventoryManager()
    3439
     40    def __unicode__(self):
     41        return self.name
     42
    3543__test__ = {'WIDGETS_TESTS': """
    3644>>> from datetime import datetime
    3745>>> from django.utils.html import escape, conditional_escape
     
    97105>>> apple = Inventory.objects.create(barcode=86, name='Apple')
    98106>>> pear = Inventory.objects.create(barcode=22, name='Pear')
    99107>>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)
     108>>> hidden = Inventory.objects.create(barcode=93, name='Hidden', hidden=True)
     109>>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden)
    100110>>> rel = Inventory._meta.get_field('parent').rel
    101111>>> w = ForeignKeyRawIdWidget(rel)
    102112>>> print w.render('test', core.parent_id, attrs={})
    103113<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
     114>>> print w.render('test', child_of_hidden.parent_id, attrs={})
     115<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Hidden</strong>
    104116""" % {
    105117    'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
    106118    'STORAGE_URL': default_storage.url(''),
Back to Top