Login

Make hyperlinks for labels of raw_id_fields (jQuery)

Author:
ramen
Posted:
December 29, 2009
Language:
JavaScript
Version:
Not specified
Tags:
admin jquery raw_id_fields
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. Hyperlink read-only ForeignKey objects in admin to their change pages by strangefeatures 3 years, 7 months ago
  2. Link raw_id_fields (both ForeignKeys and ManyToManyFields) to their change pages by EmilStenstrom 4 years, 10 months ago
  3. Admin list_display Ajax by whiteinge 7 years, 7 months ago
  4. "Save and Continue" keyboard command for admin, with autoscroll by jcushman 5 years, 7 months ago
  5. Radio widget with labels after inputs by avsd 3 years, 1 month ago

Comments

Please login first before commenting.