S
S
Stanislav Konovalov2019-05-14 21:53:06
Django
Stanislav Konovalov, 2019-05-14 21:53:06

Django. How to limit the minimum value of a numeric field in a model?

Hello. I need to make the model so that, when adding an ad, when the user enters the price, he can only do this in the format, for example: 10000, i.e. thousand, and not just 10, thinking that people will understand anyway, because the site search should find exactly 3-digit numbers and above, but not below.
So that you can enter only like this:
5cdb10520cc32021970076.png
But it’s impossible, if it means that this is also 50 thousand:
5cdb106eb7e21152445015.png
Here is a field from the model:

price = models.IntegerField(default=0, max_digits=4, blank=True, verbose_name='Цена')

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-05-14
@stas_workout

Use validator :

from django.core.validators import MinValueValidator

price = models.IntegerField(default=0,
                            max_digits=4,
                            blank=True,
                            verbose_name='Цена',
                            validators=[MinValueValidator(1000)])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question