Opened 7 years ago

Closed 5 years ago

#7985 closed (duplicate)

Cannot use the same field to prepopulate multiple fields

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

Download all attachments as: .zip

Change History (9)

Changed 7 years ago by frasern

Patch to use addEvent

comment:1 Changed 7 years ago by gwilson

  • milestone changed from 1.0 beta to 1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 7 years ago by ramikassab

  • Keywords aug22sprint added
  • Owner changed from nobody to ramikassab

Changed 7 years ago by ramikassab

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

comment:3 Changed 7 years ago by ramikassab

  • Triage Stage changed from Accepted to Ready 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 7 years ago by ramikassab

  • Status changed from new to assigned

comment:5 Changed 7 years ago by ubernostrum

  • milestone changed from 1.0 to post-1.0
  • Triage Stage changed from Ready for checkin to Accepted

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

comment:6 Changed 6 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:7 Changed 5 years ago by carljm

  • Resolution set to duplicate
  • Status changed from assigned to closed

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