diff --git a/rest_framework/fields.py b/rest_framework/fields.py index a710df7b48..107a6aeabd 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -806,6 +806,8 @@ class RegexField(CharField): def __init__(self, regex, **kwargs): super(RegexField, self).__init__(**kwargs) + if isinstance(regex, six.string_types): + regex = re.compile(regex, re.UNICODE) validator = RegexValidator(regex, message=self.error_messages['invalid']) self.validators.append(validator) diff --git a/tests/test_fields.py b/tests/test_fields.py index fc9ce192ad..49da1fde52 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -709,6 +709,20 @@ class TestiCompiledRegexField(FieldValues): field = serializers.RegexField(regex=re.compile('[a-z][0-9]')) +class TestUnicodeRegexField(FieldValues): + """ + Valid and invalid values for `RegexField`. + """ + valid_inputs = { + u'hello\u4f60\u597d': u'hello\u4f60\u597d' + } + invalid_inputs = { + u'hello\u4f60\u597d@': ["This value does not match the required pattern."] + } + outputs = {} + field = serializers.RegexField(regex='^\w*$') + + class TestSlugField(FieldValues): """ Valid and invalid values for `SlugField`.