After concatenating the contents of all the source fields, the JavaScript
behind ModelAdmin.prepopulated_fields enforces the output string's maximum
length according to the max_length of the destination field, if defined.
Unfortunately, the truncation operation currently occurs after converting
the source string into URL-safe characters and replacing whitespace with
hyphen characters. When the source string is truncated at an index
immediately following a hyphen (formerly a whitespace character), the
string returned by ModelAdmin.prepopulated_fields contains a single,
trailing hyphen that is unnecessary, meaningless, unsightly, and possibly
problematic.
Example of current behavior using URLify from
[https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js
django/contrib/admin/static/admin/js/urlify.js]:
{{{
// source_string.length === 57
source_string = 'Reading Chicken Entrails For Project Completion
Estimates';
// This length will result in source_string truncation to: 'Reading
Chicken Entrails For Project Completion '
max_length = 48;
// Outputs string 'reading-chicken-entrails-for-project-completion-'
// Note trailing hyphen.
console.log(URLify(source_string, max_length, False));
}}}
Example of new, desired behavior:
{{{
// source_string.length === 57
source_string = 'Reading Chicken Entrails For Project Completion
Estimates';
// This length will result in source_string truncation to: 'Reading
Chicken Entrails For Project Completion '
max_length = 48;
// Outputs string 'reading-chicken-entrails-for-project-completion'
// Note omission of trailing hyphen.
console.log(URLify(source_string, max_length, False));
}}}
This issue can, of course, be solved by custom
[https://docs.djangoproject.com/en/dev/ref/forms/validation/ form field
validation] but, given the spirit and purpose behind
ModelAdmin.prepopulated_fields (slug generation and convenience), I
believe the returns are worth the effort to ensure that the JavaScript
does not return trailing hyphens.
--
Ticket URL: <https://code.djangoproject.com/ticket/28295>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28295#comment:1>
Old description:
New description:
[https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields
ModelAdmin.prepopulated_fields]
After concatenating the contents of all the source fields, the JavaScript
behind ModelAdmin.prepopulated_fields enforces the output string's maximum
length according to the max_length of the destination field, if defined.
Unfortunately, the truncation operation currently occurs after converting
the source string into URL-safe characters and replacing whitespace with
hyphen characters. When the source string is truncated at an index
immediately following a hyphen (formerly a whitespace character), the
string returned by ModelAdmin.prepopulated_fields contains a single,
trailing hyphen that is unnecessary, meaningless, unsightly, and possibly
problematic.
Example of current behavior using URLify from
[https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js
django/contrib/admin/static/admin/js/urlify.js]:
{{{
// source_string.length === 57
source_string = 'Reading Chicken Entrails For Project Completion
Estimates';
// This length will result in source_string truncation to: 'Reading
Chicken Entrails For Project Completion '
max_length = 48;
// Outputs string 'reading-chicken-entrails-for-project-completion-'
// Note trailing hyphen.
console.log(URLify(source_string, max_length, false));
}}}
Example of new, desired behavior:
{{{
// source_string.length === 57
source_string = 'Reading Chicken Entrails For Project Completion
Estimates';
// This length will result in source_string truncation to: 'Reading
Chicken Entrails For Project Completion '
max_length = 48;
// Outputs string 'reading-chicken-entrails-for-project-completion'
// Note omission of trailing hyphen.
console.log(URLify(source_string, max_length, false));
}}}
This issue can, of course, be solved by custom
[https://docs.djangoproject.com/en/dev/ref/forms/validation/ form field
validation] but, given the spirit and purpose behind
ModelAdmin.prepopulated_fields (slug generation and convenience), I
believe the returns are worth the effort to ensure that the JavaScript
does not return trailing hyphens.
--
Comment (by monotonee):
Converted Python-style Boolean values to those of JavaScript.
--
Ticket URL: <https://code.djangoproject.com/ticket/28295#comment:2>
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/28295#comment:3>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"7c4f05fae2ae12d698dc28421f5d4659162f928a" 7c4f05fa]:
{{{
#!CommitTicketReference repository=""
revision="7c4f05fae2ae12d698dc28421f5d4659162f928a"
Fixed #28295 -- Made admin's URLify.js trim trailing hyphens.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28295#comment:4>