Skip to content

Commit d508b94

Browse files
committed
Registration: Validate username uniqueness by natural_key to allow for
case-insensitive usernames
1 parent 089f0bb commit d508b94

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

rest_authtoken/serializers.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,23 @@ class Meta:
2626
},
2727
}
2828

29-
def validate_password(self, value):
29+
@staticmethod
30+
def validate_username(value):
31+
"""
32+
Make sure that no user with this natural key exists.
33+
This can be used to make usernames case-insensitive, e.g.,
34+
disallow the registration of two different usernames which
35+
are equal but with different case.
36+
"""
37+
try:
38+
user = get_user_model().objects.get_by_natural_key(value)
39+
except get_user_model().DoesNotExist:
40+
return value
41+
else:
42+
raise ValidationError('already exists')
43+
44+
@staticmethod
45+
def validate_password(value):
3046
if len(value) < 6:
3147
raise ValidationError('password too short')
3248
return value

0 commit comments

Comments
 (0)