#9258 closed (fixed)
Admin ForeignKeyRawIdWidget uses wrong manager for label lookup
Reported by: | nullie | Owned by: | Brian Rosner |
---|---|---|---|
Component: | Uncategorized | Version: | 1.0 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
It results in exception when trying to edit object which has overriden objects managers.
Here's the patch:
--- django.orig/contrib/admin/widgets.py 2008-09-04 03:16:05.000000000 +0600 +++ django/contrib/admin/widgets.py 2008-10-01 12:32:18.000000000 +0600 @@ -146,7 +146,7 @@ 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 ' <strong>%s</strong>' % truncate_words(obj, 14) class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
Attachments (1)
Change History (10)
comment:1 by , 16 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 16 years ago
Needs tests: | set |
---|
comment:3 by , 16 years ago
Needs tests: | unset |
---|
Index: regressiontests/admin_widgets/models.py =================================================================== --- regressiontests/admin_widgets/models.py (revision 9218) +++ regressiontests/admin_widgets/models.py (working copy) @@ -24,14 +24,22 @@ 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) + barcode = models.PositiveIntegerField(unique=True) + parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True) + name = models.CharField(blank=False, max_length=20) - def __unicode__(self): - return self.name + default_manager = models.Manager() + objects = HiddenInventoryManager() + def __unicode__(self): + return self.name + __test__ = {'WIDGETS_TESTS': """ >>> from datetime import datetime >>> from django.utils.html import escape, conditional_escape @@ -97,10 +105,14 @@ >>> apple = Inventory.objects.create(barcode=86, name='Apple') >>> pear = Inventory.objects.create(barcode=22, name='Pear') >>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple) +>>> hidden = Inventory.objects.create(barcode=93, name='Hidden', hidden=True) +>>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden) >>> rel = Inventory._meta.get_field('parent').rel >>> w = ForeignKeyRawIdWidget(rel) >>> print w.render('test', core.parent_id, attrs={}) <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> <strong>Apple</strong> +>>> print w.render('test', child_of_hidden.parent_id, attrs={}) +<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> <strong>Hidden</strong> """ % { 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX, 'STORAGE_URL': default_storage.url(''),
comment:4 by , 16 years ago
Has patch: | unset |
---|
Please attach a patch file. It's not very nice to have and cut-and-paste things out of comments.
It's not correct to check "has_patch" when there's no file attached.
by , 16 years ago
Attachment: | foreing-key-raw-id-admin.diff added |
---|
comment:5 by , 16 years ago
Has patch: | set |
---|
comment:6 by , 16 years ago
Owner: | changed from | to
---|
Brian, I took liberty to assign this to you since you once said that it's good to have it in 1.0.1. Given that there was no reaction to the latest patch I was afraid it could slip off the schedule.
comment:7 by , 16 years ago
Status: | new → assigned |
---|
comment:8 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:9 by , 16 years ago
Note:
See TracTickets
for help on using tickets.
Add on a regression test and it's good to go.