Avoided hardcoding Permission.name max_length

refs #18866.
This commit is contained in:
Tim Graham 2013-10-16 11:29:29 -04:00
parent 3918eeb9fd
commit 91c77eeab8
1 changed files with 8 additions and 2 deletions

View File

@ -101,10 +101,16 @@ def create_permissions(app, created_models, verbosity, db=DEFAULT_DB_ALIAS, **kw
]
# Validate the permissions before bulk_creation to avoid cryptic
# database error when the verbose_name is longer than 50 characters
permission_name_max_length = auth_app.Permission._meta.get_field('name').max_length
verbose_name_max_length = permission_name_max_length - 11 # len('Can change ') prefix
for perm in perms:
if len(perm.name) > 50:
if len(perm.name) > permission_name_max_length:
raise exceptions.ValidationError(
"The verbose_name of %s is longer than 39 characters" % perm.content_type)
"The verbose_name of %s is longer than %s characters" % (
perm.content_type,
verbose_name_max_length,
)
)
auth_app.Permission.objects.using(db).bulk_create(perms)
if verbosity >= 2:
for perm in perms: