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
patch