Opened 8 years ago

Closed 7 years ago

#7985 closed (duplicate)

Cannot use the same field to prepopulate multiple fields

Reported by: Fraser Nevett Owned by: Rami Kassab
Component: contrib.admin Version: master
Severity: Keywords: aug22sprint
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Take, for example, this ModelAdmin:

class ExampleAdmin(admin.ModelAdmin):
    prepopulated_fields = {
       "slug": ("name",),
       "full_slug": ("name", "category"),
       }

It results in the following Javascript being generated in the template:

    document.getElementById("id_full_slug").onchange = function() { this._changed = true; };
    
    document.getElementById("id_name").onkeyup = function() {
        var e = document.getElementById("id_full_slug");
        if (!e._changed) { e.value = URLify(document.getElementById("id_name").value + ' ' + document.getElementById("id_category").value, 50); }
    }
    
    document.getElementById("id_category").onkeyup = function() {
        var e = document.getElementById("id_full_slug");
        if (!e._changed) { e.value = URLify(document.getElementById("id_name").value + ' ' + document.getElementById("id_category").value, 50); }
    }
    

    document.getElementById("id_slug").onchange = function() { this._changed = true; };
    
    document.getElementById("id_name").onkeyup = function() {
        var e = document.getElementById("id_slug");
        if (!e._changed) { e.value = URLify(document.getElementById("id_name").value, 50); }
    }

The cause of the problem is the event listeners being attached by simply setting the onkeyup property. This means the second assignment overwrites the first one.

To attach multiple event listeners, the addEvent function defined in core.js should be used.

Attachments (2)

7985.patch (1.2 KB) - added by Fraser Nevett 8 years ago.
Patch to use addEvent
7985-modified.patch (1.2 KB) - added by Rami Kassab 8 years ago.
Modified patch since the JS the first patch refers to has been moved…

Download all attachments as: .zip

Change History (9)

Changed 8 years ago by Fraser Nevett

Attachment: 7985.patch added

Patch to use addEvent

comment:1 Changed 8 years ago by Gary Wilson

milestone: 1.0 beta1.0
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 8 years ago by Rami Kassab

Keywords: aug22sprint added
Owner: changed from nobody to Rami Kassab

Changed 8 years ago by Rami Kassab

Attachment: 7985-modified.patch added

Modified patch since the JS the first patch refers to has been moved...

comment:3 Changed 8 years ago by Rami Kassab

Triage Stage: AcceptedReady for checkin

Confirmed this minor bug. Currently, with 'name' being used to pre-populate two fields, 'slug' will be pre-populated while typing; however, 'full_slug' won't be pre-populated until the user tabs away from 'name'. If the user clicks on a different field instead of tabs, 'full_slug' will never be pre-populated with name. This patch fixes the issue; however, I had to attach a modified version since the location of the JS that the original patch modified has been moved to 'django/contrib/admin/templates/admin/prepopulated_fields_js.html'.

comment:4 Changed 8 years ago by Rami Kassab

Status: newassigned

comment:5 Changed 8 years ago by James Bennett

milestone: 1.0post-1.0
Triage Stage: Ready for checkinAccepted

This is relatively minor and I think we can safely punt it in favor of more critical work.

comment:6 Changed 8 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:7 Changed 7 years ago by Carl Meyer

Resolution: duplicate
Status: assignedclosed

This is a duplicate of #9264 - closing this one since Jacob marked the patch on #9264 RFC. I suspect both patches might get superseded by a jQuery-based reworking anyway, but I'll add a note on #9264 to check the patch here.

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