Opened 18 years ago

Closed 18 years ago

Last modified 17 years ago

#620 closed defect (fixed)

[patch] make limit_choices_to work with raw_id_admin

Reported by: davidschein@… Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With raw_id_admin=True, the admin UI does not limit choices as per limit_choices_to. The following patch fixes it.

Index: django/conf/admin_media/js/admin/RelatedObjectLookups.js

===================================================================

--- django/conf/admin_media/js/admin/RelatedObjectLookups.js	(revision 851)

+++ django/conf/admin_media/js/admin/RelatedObjectLookups.js	(working copy)

@@ -3,7 +3,13 @@

 
 function showRelatedObjectLookupPopup(triggeringLink) {
     var name = triggeringLink.id.replace(/^lookup_/, '');
-    var win = PrivoxyWindowOpen(triggeringLink.href + '?pop=1', name, 'height=500,width=740,resizable=yes,scrollbars=yes');
+    var href
+    if (triggeringLink.href.search(/\?/) >= 0) {
+        href = triggeringLink.href + '&pop=1';
+    } else {
+        href = triggeringLink.href + '?pop=1'
+    }
+    var win = PrivoxyWindowOpen(href, name, 'height=500,width=740,resizable=yes,scrollbars=yes');
     win.focus();
     return false;
 }
Index: django/views/admin/main.py

===================================================================

--- django/views/admin/main.py	(revision 851)

+++ django/views/admin/main.py	(working copy)

@@ -746,8 +746,12 @@

     field_id = 'id_%s%s' % ((rel and "%s{{ forloop.counter0 }}." % name_prefix or ""), field.get_manipulator_field_names('')[0])
     # raw_id_admin fields get the little lookup link next to them
     if use_raw_id_admin(field):
-        t.append(' <a href="../../../%s/%s/" class="related-lookup" id="lookup_%s" onclick="return showRelatedObjectLookupPopup(this);">' % \
-                    (field.rel.to.app_label, field.rel.to.module_name, field_id))
+        if field.rel.limit_choices_to:
+            limit_choices_to = '?%s' % '&'.join(['%s=%s' % (k,v) for k,v in field.rel.limit_choices_to.items()])
+        else:
+            limit_choices_to = ''
+        t.append(' <a href="../../../%s/%s/%s" class="related-lookup" id="lookup_%s" onclick="return showRelatedObjectLookupPopup(this);">' % \
+                    (field.rel.to.app_label, field.rel.to.module_name, limit_choices_to, field_id))
         t.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % ADMIN_MEDIA_PREFIX)
     # fields with relationships to editable objects get an "add another" link,
     # but only if the field doesn't have raw_admin ('cause in that case they get

Attachments (2)

admin_limit_choices.patch (2.5 KB ) - added by davidschein@… 18 years ago.
patch
admin_limit_choices.2.patch (2.4 KB ) - added by davidschein@… 18 years ago.
a little neater patch

Download all attachments as: .zip

Change History (5)

comment:1 by anonymous, 18 years ago

Summary: make limit_choices_to work with raw_id_admin[patch] make limit_choices_to work with raw_id_admin

by davidschein@…, 18 years ago

Attachment: admin_limit_choices.patch added

patch

comment:2 by davidschein@…, 18 years ago

I updated this patch (I just attached it) to work with the new admin. It is really helpful. Honestly. Well, for me anyhow.

by davidschein@…, 18 years ago

Attachment: admin_limit_choices.2.patch added

a little neater patch

comment:3 by Jacob, 18 years ago

Resolution: fixed
Status: newclosed

(In [2414]) Fixed #620 -- raw_id_admin and limit_choices_to now play well together (thanks to David Schein)

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