T
T
tarp202021-04-15 14:07:47
Django
tarp20, 2021-04-15 14:07:47

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])


created validators=[validate_color]

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


but for some reason it does not work, maybe you need to add something ??

error in validate_color

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2021-04-15
@tumbler

>>> False == None
False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question