Django

Code

Ticket #7985 (closed: duplicate)

Opened 2 years ago

Last modified 5 days ago

Cannot use the same field to prepopulate multiple fields

Reported by: frasern Assigned to: ramikassab
Milestone: Component: django.contrib.admin
Version: SVN Keywords: aug22sprint
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

7985.patch (1.2 kB) - added by frasern on 07/27/08 07:22:40.
Patch to use addEvent
7985-modified.patch (1.2 kB) - added by ramikassab on 08/22/08 19:15:58.
Modified patch since the JS the first patch refers to has been moved…

Change History

07/27/08 07:22:40 changed by frasern

  • attachment 7985.patch added.

Patch to use addEvent

07/31/08 23:25:22 changed by gwilson

  • needs_better_patch changed.
  • needs_docs changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • milestone changed from 1.0 beta to 1.0.

08/22/08 18:05:02 changed by ramikassab

  • keywords set to aug22sprint.
  • owner changed from nobody to ramikassab.

08/22/08 19:15:58 changed by ramikassab

  • attachment 7985-modified.patch added.

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

08/22/08 19:20:41 changed by ramikassab

  • 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'.

08/22/08 19:26:38 changed by ramikassab

  • status changed from new to assigned.

08/22/08 21:47:16 changed by ubernostrum

  • stage changed from Ready for checkin to Accepted.
  • milestone changed from 1.0 to post-1.0.

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

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted

03/16/10 21:09:23 changed by carljm

  • status changed from assigned to closed.
  • resolution set to duplicate.

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.


Add/Change #7985 (Cannot use the same field to prepopulate multiple fields)




Change Properties
Action