szafranskid
szafranskid

Reputation: 13

Django use model field's validators for client-side validation

I have some example model with field:

class Task(models.Model):
    priority = models.IntegerField(default=30, validators=[MinValueValidator(0), MaxValueValidator(300)])

When I create a ModelForm object by modelform_factory() function and generate html with .as_table() function then for this field I get something like:

<input type="number" name="priority" value="30" required="" id="id_priority">

But I would like the output to look like this:

<input type="number" name="priority" value="30" required="" id="id_priority" min="0" max="300">

What is the best way to do that?

Upvotes: 1

Views: 422

Answers (1)

Surajano
Surajano

Reputation: 2688

have you added proper Import ?

from django.core.validators import MaxValueValidator, MinValueValidator

priority = models.IntegerField(default=30, validators=[MinValueValidator(0), MaxValueValidator(300)])

Upvotes: 1

Related Questions