Opened 15 years ago
Closed 13 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)
Change History (9)
Changed 15 years ago by
Attachment: | 7985.patch added |
---|
comment:1 Changed 15 years ago by
milestone: | 1.0 beta → 1.0 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 Changed 14 years ago by
Keywords: | aug22sprint added |
---|---|
Owner: | changed from nobody to Rami Kassab |
Changed 14 years ago by
Attachment: | 7985-modified.patch added |
---|
Modified patch since the JS the first patch refers to has been moved...
comment:3 Changed 14 years ago by
Triage Stage: | Accepted → 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 14 years ago by
Status: | new → assigned |
---|
comment:5 Changed 14 years ago by
milestone: | 1.0 → post-1.0 |
---|---|
Triage Stage: | Ready for checkin → Accepted |
This is relatively minor and I think we can safely punt it in favor of more critical work.
comment:7 Changed 13 years ago by
Resolution: | → duplicate |
---|---|
Status: | assigned → closed |
Patch to use addEvent