fromdjango.core.exceptionsimportValidationErrorfromdjango.utils.functionalimportSimpleLazyObjectfromdjango.utils.textimportformat_lazydefprefix_validation_error(error,prefix,code,params):""" Prefix a validation error message while maintaining the existing validation data structure. """iferror.error_list==[error]:error_params=error.paramsor{}returnValidationError(# We can't simply concatenate messages since they might require# their associated parameters to be expressed correctly which# is not something `format_lazy` does. For example, proxied# ngettext calls require a count parameter and are converted# to an empty string if they are missing it.message=format_lazy("{} {}",SimpleLazyObject(lambda:prefix%params),SimpleLazyObject(lambda:error.message%error_params),),code=code,params={**error_params,**params},)returnValidationError([prefix_validation_error(e,prefix,code,params)foreinerror.error_list])