How can I have only positive decimal numbers in Django using Python

0 votes

Hi all, pretty simple question but I seem to be stuck.

Let us say I have created a few models in Django and I ended up creating a decimal field that looks something like this. Check it out:

price = models.DecimalField(_(u'Price'), decimal_places=2, max_digits=12)

We already know that price cannot be negative or in fact, it cannot be zero either. So, my question is simple. Is there any way which I can make use of to limit the number to make sure it is only positive?

Is it that or would I have to consider making use of form validation to obtain this?

All help appreciated!

Jan 30, 2019 in Python by Anirudh
• 2,080 points
4,860 views

1 answer to this question.

0 votes

Hi, are you aware of something called as the MinValueValidator

Consider the following example:

price = models.DecimalField(_(u'Price'), decimal_places=2, max_digits=12, validators=[MinValueValidator(Decimal('0.01'))])

You could go about using that or you could just add min_value=Decimal('0.01')

Either way, your purpose should be solved using this. Cheers!

answered Jan 30, 2019 by Nymeria
• 3,560 points

Related Questions In Python

0 votes
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
20,913 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,710 points
11,345 views
0 votes
1 answer

How can I define a multidimensional array in python using ctype?

Here's one quick-and-dirty method: >>> A = ((ctypes.c_float ...READ MORE

answered Oct 9, 2018 in Python by aryya
• 7,440 points
3,180 views
0 votes
2 answers

How can I rename multiple files in a certain directory using Python?

import os from optparse import OptionParser, Option class MyOption ...READ MORE

answered Jul 29, 2020 in Python by The real slim shady
3,877 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,136 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
2,335 views
0 votes
1 answer

How can I prevent brute force login attacks using Django in Python?

Hi. Django-axes is an already existing application ...READ MORE

answered Feb 15, 2019 in Python by Nymeria
• 3,560 points
1,846 views
0 votes
1 answer

How can I read numbers in Python from a custom file?

Hi, good question. Let us first assume that ...READ MORE

answered Feb 6, 2019 in Python by Nymeria
• 3,560 points
627 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP