Simple improvement to raw_id_admin

60 views
Skip to first unread message

andre...@gmail.com

unread,
Jun 29, 2006, 5:00:30 PM6/29/06
to Django developers
I have some objects that refer another class that currently has about
3000 instances, so the default drop-down menu for ForeignKey breaks
down completely. I then found the raw_admin option, but this has a
major weakness, it doesn't change the text describing the selected item
when you pick a new, just the id value. This results in a very poor
user experience (I myself didn't see the id value change until I had
tried 3 times). I created a very quick and dirty fix for this, just to
see that it works, by simply submitting the str() representation of the
related object back via the dismissRelatedLookupPopup() function, and
replacing the innerHTML attribute of the text element holding the name
of the related object. I also had to add a placeholder
<strong></strong> tag for when the user is creating a new item.

I should probably mention that I've been using Django for a total of
something like 20 work hours :) I'm not sure this follows your style of
code, and there's quite possibly things I haven't thought about, but
given that I think this is a very useful enhancement that was very
quick to add I was hoping that you might consider incorporating it.
I've included the svn diff output below, but it will be mangled as I
post this message I guess. I'm not expecting this code to make it into
the actual source, but I would very much appreciate it if the feature
itself was added by someone.

Index: contrib/admin/media/js/admin/RelatedObjectLookups.js
===================================================================
--- contrib/admin/media/js/admin/RelatedObjectLookups.js
(revision 3237)
+++ contrib/admin/media/js/admin/RelatedObjectLookups.js
(working copy)
@@ -16,14 +16,18 @@
return false;
}

-function dismissRelatedLookupPopup(win, chosenId) {
+function dismissRelatedLookupPopup(win, chosenId, chosenName) {
var name = win.name.replace(/___/g, '.');
var elem = document.getElementById(name);
+ var nameElem = document.getElementById("view_" + name);
if (elem.className.indexOf('vRawIdAdminField') != -1 &&
elem.value) {
elem.value += ',' + chosenId;
} else {
document.getElementById(name).value = chosenId;
}
+ if(nameElem) {
+ nameElem.innerHTML = chosenName;
+ }
win.close();
}

Index: contrib/admin/templatetags/admin_list.py
===================================================================
--- contrib/admin/templatetags/admin_list.py (revision 3237)
+++ contrib/admin/templatetags/admin_list.py (working copy)
@@ -169,8 +169,9 @@
first = False
url = cl.url_for_result(result)
result_id = str(getattr(result, pk)) # str() is needed in
case of 23L (long ints)
+ result_name = str(result)
yield ('<th%s><a href="%s"%s>%s</a></th>' % \
- (row_class, url, (cl.is_popup and '
onclick="opener.dismissRelatedLookupPopup(window, %r); return false;"'
% result_id or ''), result_repr))
+ (row_class, url, (cl.is_popup and '
onclick="opener.dismissRelatedLookupPopup(window, %r, %r); return
false;"' % (result_id or '', result_name)), result_repr))
else:
yield ('<td%s>%s</td>' % (row_class, result_repr))

Index: contrib/admin/templates/widget/foreign.html
===================================================================
--- contrib/admin/templates/widget/foreign.html (revision 3237)
+++ contrib/admin/templates/widget/foreign.html (working copy)
@@ -10,11 +10,14 @@
{% if bound_field.needs_add_label %}
<a href="{{ bound_field.related_url }}add/" class="add-another"
id="add_{{ bound_field.element_id }}" onclick="return
showAddAnotherPopup(this);"> <img src="{% admin_media_prefix
%}img/admin/icon_addlink.gif" width="10" height="10" alt="Add
Another"/></a>
{% endif %}{% endif %}
+{% if not change %}
+ <strong id="view_{{ bound_field.element_id }}"></strong>
+{% endif %}
{% if change %}
{% if bound_field.field.primary_key %}
{{ bound_field.original_value }}
{% endif %}
{% if bound_field.raw_id_admin %}
- {% if bound_field.existing_display %}&nbsp;<strong>{{
bound_field.existing_display|truncatewords:"14" }}</strong>{% endif %}
+ {% if bound_field.existing_display %}&nbsp;<strong id="view_{{
bound_field.element_id }}">{{
bound_field.existing_display|truncatewords:"14" }}</strong>{% endif %}
{% endif %}
{% endif %}

Reply all
Reply to author
Forward
0 new messages