Login

Make hyperlinks for labels of raw_id_fields (jQuery)

Author:
ramen
Posted:
December 29, 2009
Language:
JavaScript
Version:
Not specified
Score:
0 (after 0 ratings)

The Django Admin site provides a "raw ID" feature for foreign keys that reference large tables. In the form view, the label of the referenced object will appear after the input. This snippet will make that label into a hyperlink that takes you to the form view for that object.

To use this snippet, copy base_site.html into your templates/admin directory (if you haven't already), and paste this code into that file somewhere after the 'extends "admin/base.html"' directive. If you are already using jQuery, you can remove the first script tag. If you are already extending "extrahead", just paste the script tag(s) into the block you have created.

Documentation for raw_id_fields

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{% block extrahead %}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
//<![CDATA[
    jQuery(function($) {
        // Make hyperlinks for labels of raw_id_fields
        $('a.related-lookup').each(function() {
            var td = $(this).parent();
            var id = $('input', td).val();
            var url = $(this).attr('href').split('?')[0] + id + '/';
            $('strong', td).wrap('<a href="' + url + '"></a>');
        });
    });
//]]>
</script>
{% endblock %}

More like this

  1. Django Collapsed Stacked Inlines by applecat 1 year, 9 months ago
  2. Django Collapsed Stacked Inlines by mkarajohn 3 years, 11 months ago
  3. Dynamically adding forms to a formset. OOP version. by halfnibble 9 years, 7 months ago
  4. Convert multiple select for m2m to multiple checkboxes in django admin form by abidibo 11 years, 8 months ago
  5. Django admin inline ordering - javascript only implementation by ojhilt 12 years ago

Comments

Please login first before commenting.