Kit*_*Kit 1 python django django-rest-framework
希望你们帮我配置 RegexField 仅在 Django Rest Framework 中接受连字符 (-) 和数字
现在我配置:
username = RegexField(regex=r'^[\d-]+$', required=True, max_length=50)
我的序列化器:
class UserSignUpSerializer(ModelSerializer):
username = RegexField(regex=r'^[\d-]+$', required=True, max_length=50)
email = EmailField(required=True, allow_blank=False, max_length=50)
password = CharField(required=True, allow_blank=False, max_length=20)
class Meta:
model = User
fields = [
'username',
'email',
'password'
]
extra_kwargs = {"password": {"write_only": True}}
Run Code Online (Sandbox Code Playgroud)
这不是工作!
提前致谢!
该正则表达式允许任何字母数字字符(a to z
、A to Z
和0 to 9
)。
您正在寻找:
# \d allows only numeric digits
# - or \- allows only hyphen
# [\d-] allows a single character, either a digit or a hyphen
# [\d-]+ allows for more than one character, but a minimum of one is required
# ^[\d-]+$ the whole input string must match the regular expression
RegexField(regex=r'^[\d-]+$', required=True, max_length=50)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1495 次 |
最近记录: |