Django Field Regex Validation -
i trying create model store hashtag.
the validator doesn't seem working, making field accept inputs, , can't find solution.
here model:
class hashtags(models.model): hashtag_validator = regexvalidator(r'^[#](\w+)$', "hashtag doesn't comply.") hashtag_id = models.autofield(primary_key=true) hashtag_text = models.charfield(max_length=100, validators=[hashtag_validator], unique=true) def get_id(self): return self.hashtag_id def get_text(self): return self.hashtag_text
you can alter below given code see working
hashtag_validator = charfield( max_length=50, required=true, #if want field mandatory validators=[ regexvalidator( regex='^[#](\w+)$', message='hashtag doesnt comply', ), ] )
hope helps!!
if causing problem can try writing own validator
from django.core.exceptions import validationerror import re def validate_hash(value): reg = re.compile('^[#](\w+)$') if !reg.match(value) : raise validationerror(u'%s hashtag doesnot comply' % value)
and change model field
hashtag_validator = models.charfield(validators=[validate_hash])
Comments
Post a Comment