admin: edit related object shortcut

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<script type="text/javascript">
    $(function(){
        $("select+a.add-another").each(function(){
            $(this).after("&nbsp;<a class='changelink' href='#'></a>");
            $(this).next().click(function(){
                var link = ($(this).prev().attr('href')+'../'+$(this).prev().prev().attr('value'));
                var win = window.open(link + '?_popup=1', link, 'height=600,width=1000,resizable=yes,scrollbars=yes');
                win.focus();
                return false;
            });
        });
    });
</script>

More like this

  1. Add extra form elements in your contib admin by sergejdergatsjev 5 years, 4 months ago
  2. Allow editing of the selected object of the filter_vertical/filter_horizontal widget with jquery by jpic 5 years, 6 months ago
  3. Filter change list by a date range by aruseni 2 years, 2 months ago
  4. Add special field lookups to the Admin list_filter display by whiteinge 6 years, 2 months ago
  5. Admin Hack: Quick lookup of GenericForeignKey id by ContentType by adnan 5 years, 7 months ago

Comments

magicrebirth (on April 21, 2009):

Thanks! That's exactly what I was looking for...

I modified it a little so that if nothing's selected in the FK field it just opens the generic 'select object to change' popup (instead of throwing an error).

` $(function(){

    $("select+a.add-another").each(function(){
        $(this).after("&nbsp;<a class='changelink' href='#'></a>");
        $(this).next().click(function(){

            var linkvalue = $(this).prev().prev().attr('value');
            if (linkvalue) {
                var linkname = ($(this).prev().attr('href')+'../'+linkvalue);   
                var link = linkname + '?_popup=1';
            }  else  {
                var linkname = ($(this).prev().attr('href').replace("add/", ""));
                var link = linkname + "?t=id&pop=1";
            }

            var win = window.open(linkname, link, 'height=600,width=1000,resizable=yes,scrollbars=yes');
            win.focus();
            return false;

        });
    });

`

#

marcob (on August 14, 2009):

With this two lines patch, Save button close the window.

diff -r 5d02762d8f3b django/contrib/admin/options.py
--- django/contrib/admin/options.py Thu Aug 13 12:54:32 2009 +0200
+++ django/contrib/admin/options.py Fri Aug 14 13:45:36 2009 +0200
@@ -632,6 +632,9 @@
         pk_value = obj._get_pk_val()

         msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)}
+
+        if request.POST.has_key("_popup"):
+            return HttpResponse('<script type="text/javascript">window.close();</script>')
         if request.POST.has_key("_continue"):
             self.message_user(request, msg + ' ' + _("You may edit it again below."))
             if request.REQUEST.has_key('_popup'):

#

nemesis (on January 28, 2010):

Two little changes: added a title attribute to the edit icon and a simple if that prevents opening a new window in case nothing is selected:

$(function(){
    $("select+a.add-another").each(function(){
        $(this).after("&nbsp;<a class='changelink' href='#' title='Edit slected item'></a>");
        $(this).next().click(function(){
            if($(this).prev().prev().attr('value')=='') return false;
            var link = ($(this).prev().attr('href')+'../'+$(this).prev().prev().attr('value'));
            var win = window.open(link + '?_popup=1', link, 'height=600,width=1000,resizable=yes,scrollbars=yes');
            win.focus();
            return false;
        });
    });
});

#

lee (on September 27, 2011):

Probably a noob problem, but the snippet has no effect for me with Django 1.3. The script shows up in the head section, and I've added the jquery line, but no markup is added after the add-another links. Any ideas?

#

lee (on September 27, 2011):

Scratch my comment, I had the wrong path to my jquery.js. Snippet works great under 1.3.

#

sleytr (on March 2, 2012):

Modified for rawid fields. Converted $s to django.jQuery for placing it into change_form.html. Works with Django 1.4b1.

    django.jQuery(window).ready(function () {
        django.jQuery('.vForeignKeyRawIdAdminField').each(function(){
            var fkf =django.jQuery(this)
            if(fkf.val()){
                var editlink = fkf.next()
                var name = editlink.next()
                var hrf = editlink.attr('href').split('?')[0] + fkf.val()
                name.after("&nbsp;<a class='changelink' href='"+hrf+"'></a>");
            }
        })
        django.jQuery("select+a.add-another").each(function(){
            django.jQuery(this).after("&nbsp;<a class='changelink' href='#'></a>");
            django.jQuery(this).next().click(function(){

                var linkvalue = django.jQuery(this).prev().prev().attr('value');
                if (linkvalue) {
                    var linkname = (django.jQuery(this).prev().attr('href')+'../'+linkvalue);
                    var link = linkname + '?_popup=1';
                }  else  {
                    var linkname = (django.jQuery(this).prev().attr('href').replace("add/", ""));
                    var link = linkname + "?t=id&pop=1";
                }

                var win = window.open(linkname, link, 'height=600,width=1000,resizable=yes,scrollbars=yes');
                win.focus();
                return false;

            });
        });})

#

darklow (on February 5, 2013):

Another workaround, if want to close popup window after save and you don't want to patch django/contrib/admin/options/.py

Extend original admin/change_list.html in following way.

{% extends "admin/change_list.html" %}

{% block js_foot %}
    {{ block.super }}
    <script type="text/javascript">
    if(document.referrer.indexOf('_popup') != -1) {
        self.close()
    }
    </script>
{% endblock %}

#

(Forgotten your password?)