Answer the question
In order to leave comments, you need to log in
How to create custom model validator in django??
class Car(models.Model):
unique_id = models.UUIDField(default=uuid.uuid4(), editable=False)
make = models.CharField(max_length=56, blank=False, null=False)
color = models.CharField(max_length=56, blank=False, null=False, validators=[validate_color])
validators=[validate_color]
def validate_color(color):
match = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', color)
if match == False:
raise ValidationError(' Not correct color')
else:
return color
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question