我有这个型号的
name = models.CharField(max_length=50, blank=True, null=True)
email = models.EmailField(max_length=50, unique=True)
我希望用户不应该能够在这两个字段中使用字母数字以外的任何其他字符.
有什么办法吗?
我有这个型号的
name = models.CharField(max_length=50, blank=True, null=True)
email = models.EmailField(max_length=50, unique=True)
我希望用户不应该能够在这两个字段中使用字母数字以外的任何其他字符.
有什么办法吗?
您可以使用validator来限制字段接受的内容.RegexValidator
可以在这里做到这一点:
from django.core.validators import RegexValidator
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', 'Only alphanumeric characters are allowed.')
name = models.CharField(max_length=50, blank=True, null=True, validators=[alphanumeric])
email = models.EmailField(max_length=50, unique=True, validators=[alphanumeric])
注意,已经有一个validate_email
validator可以为您验证邮箱地址;上面的alphanumeric
验证器不允许有效的邮箱地址.