Opened 16 years ago

Closed 14 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: dev
Severity: Keywords: aug22sprint
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 16 years ago.
Patch to use addEvent
7985-modified.patch (1.2 KB ) - added by Rami Kassab 16 years ago.
Modified patch since the JS the first patch refers to has been moved…

Download all attachments as: .zip

Change History (9)

by Fraser Nevett, 16 years ago

Attachment: 7985.patch added

Patch to use addEvent

comment:1 by Gary Wilson, 16 years ago

milestone: 1.0 beta1.0
Triage Stage: UnreviewedAccepted

comment:2 by Rami Kassab, 16 years ago

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

by Rami Kassab, 16 years ago

Attachment: 7985-modified.patch added

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

comment:3 by Rami Kassab, 16 years ago

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 by Rami Kassab, 16 years ago

Status: newassigned

comment:5 by James Bennett, 16 years ago

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 by (none), 15 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:7 by Carl Meyer, 14 years ago

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