Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#9258 closed (fixed)

Admin ForeignKeyRawIdWidget uses wrong manager for label lookup

Reported by: nullie Owned by: brosner
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: UI/UX:

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 7 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 7 years ago by brosner

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 7 years ago by SmileyChris

  • Needs tests set

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

comment:3 Changed 7 years ago by nullie

  • 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 Changed 7 years ago by mtredinnick

  • 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.

Changed 7 years ago by nullie

comment:5 Changed 7 years ago by nullie

  • Has patch set

comment:6 Changed 7 years ago by isagalaev

  • Owner changed from nobody to brosner

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 Changed 7 years ago by brosner

  • Status changed from new to assigned

comment:8 Changed 7 years ago by brosner

  • Resolution set to fixed
  • Status changed from assigned to closed

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

comment:9 Changed 7 years ago by brosner

(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