You would use a validator to limit what the field accepts. A RegexValidator
would do the trick here:
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])
Note that there already is a validate_email
validator that'll validate email addresses for you; the alphanumeric
validator above will not allow for valid email addresses.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…