Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#26387 closed Bug (fixed)

Related popup doesn’t open in a changelist when list_editable in raw_id_fields

Reported by: Bertrand Bordage Owned by: Tim Graham
Component: contrib.admin Version: 1.8
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

Suppose you have this admin class, where 'fk' is a ForeignKey field name:

@register(YourModel)
class YourModelAdmin(ModelAdmin):
    list_display = ['pk', 'fk']
    list_editable = ['fk']
    raw_id_fields = ['fk']

When you open the changelist, everything is displayed normally, fk consists in a field and a magnifying glass. But no javascript 'click' event is bound to the glass, so the changelist of the related model opens directly in the main window, not in popup.

Of course, it prevents the users from filling the field, the only solutions being: to type the ID, or to open the change view, which may be disabled if you want users to only use the changelist…

Bug experienced on all Django 1.9 versions, and probably master.

Change History (8)

comment:1 by Tim Graham, 9 years ago

Description: modified (diff)
Severity: NormalRelease blocker
Summary: Related popup doesn’t open in a changelist when in raw_id_fieldsRelated popup doesn’t open in a changelist when list_editable in raw_id_fields
Triage Stage: UnreviewedAccepted
Version: 1.91.8

I found it necessary to include another field in list_display to reproduce this. Otherwise on master, you'll get an error like "The value of 'list_editable[0]' refers to the first field in 'list_display' ('question'), which cannot be used unless 'list_display_links' is set." I edited the ticket description to include this.

Seems to be a regression in Django 1.8 the popup works on 1.7.

comment:2 by Bertrand Bordage, 9 years ago

The solution would be to partially include admin/js/change_form.js to the changelist view.

I made a workaround for Django 1.9 (and probably all other versions). Create a static file admin/js/change_list.js containing this:

// FIXME: Remove this file, it’s a workaround to https://code.djangoproject.com/ticket/26387

(function($) {
    'use strict';
    $(document).ready(function() {
        $('.related-lookup').click(function(e) {
            e.preventDefault();
            var event = $.Event('django:lookup-related');
            $(this).trigger(event);
            if (!event.isDefaultPrevented()) {
                showRelatedObjectLookupPopup(this);
            }
        });
    });
})(django.jQuery);

Then add a media property to your admin class:

from django.forms import Media

class YourModelAdmin(ModelAdmin):
    []

    # FIXME: Remove this workaround to https://code.djangoproject.com/ticket/26387
    @property
    def media(self):
        return super(YourModelAdmin,
                     self).media + Media(js=['admin/js/change_list.js'])

comment:3 by Tim Graham, 9 years ago

Owner: changed from nobody to Tim Graham
Status: newassigned

comment:4 by Tim Graham, 9 years ago

Has patch: set

comment:5 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In acfaec3d:

Fixed #26387 -- Restored the functionality of the admin's raw_id_fields in list_editable.

comment:6 by Tim Graham <timograham@…>, 9 years ago

In 1f15d44:

[1.9.x] Fixed #26387 -- Restored the functionality of the admin's raw_id_fields in list_editable.

Backport of acfaec3db5ba39de52f6e607e74343dccf72fba1 from master

comment:7 by Tim Graham <timograham@…>, 9 years ago

In 0496838e:

[1.8.x] Fixed #26387 -- Restored the functionality of the admin's raw_id_fields in list_editable.

Backport of acfaec3db5ba39de52f6e607e74343dccf72fba1 from master

comment:8 by Bertrand Bordage, 9 years ago

Thank you very much for your work, Tim ! :)

Note: See TracTickets for help on using tickets.
Back to Top