﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28295	ModelAdmin.prepopulated_fields generates a string with a trailing hyphen	monotonee	monotonee	"[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."	Cleanup/optimization	closed	contrib.admin	1.11	Normal	fixed	admin, ModelAdmin, prepopulated_fields, urlify, slug		Accepted	1	0	1	0	0	0
