Opened 16 years ago

Closed 15 years ago

Last modified 15 years ago

#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 '&nbsp;<strong>%s</strong>' % truncate_words(obj, 14)
 
 class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):

Attachments (1)

foreing-key-raw-id-admin.diff (3.2 KB ) - added by nullie 16 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 by Brian Rosner, 16 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Chris Beaven, 16 years ago

Needs tests: set

Add on a regression test and it's good to go.

comment:3 by nullie, 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>&nbsp;<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>&nbsp;<strong>Hidden</strong>
 """ % {
     'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
     'STORAGE_URL': default_storage.url(''),

comment:4 by Malcolm Tredinnick, 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 nullie, 16 years ago

comment:5 by nullie, 16 years ago

Has patch: set

comment:6 by Ivan Sagalaev, 16 years ago

Owner: changed from nobody to Brian Rosner

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 Brian Rosner, 16 years ago

Status: newassigned

comment:8 by Brian Rosner, 15 years ago

Resolution: fixed
Status: assignedclosed

(In [9444]) Fixed #9258 -- Use _default_manager in ForeignKeyRawIdWidget.label_for_value. Thanks nullie for the patch.

comment:9 by Brian Rosner, 15 years ago

(In [9445]) [1.0.X] Fixed #9258 -- Use _default_manager in ForeignKeyRawIdWidget.label_for_value. Thanks nullie for the patch.

Backport of r9444 from trunk.

Note: See TracTickets for help on using tickets.
Back to Top