Issue
I'm trying to build a models.DecimalField
which will set to 50 and will have a range (from 50 to 9000) with a step 50. I have read that I can use choices
: "A sequence consisting itself of iterables of exactly two items (e.g. [(A, B), (A, B) ...]) to use as choices for this field. If choices are given, they’re enforced by model validation and the default form widget will be a select box with these choices instead of the standard text field.".
I have no idea how to fix it. For sure error is in quantity = models.DecimalField
.
My code:
from django.db import models
# Create your models here.
class ProductInquirie(models.Model):
email = models.CharField(null=False, blank=False, max_length=120) # max_lenght required
tittle = models.TextField(null=False, blank=False)
quantity = models.DecimalField(
null=True,
blank=True,
decimal_places=0,
max_digits=4,
default=50,
choices=[(i, i) for i in range(50, 9000, 50)]
)
target_price = models.DecimalField(null=False, blank=True, decimal_places=2, max_digits=7) # Why decimal not float: https://docs.python.org/3/library/decimal.html#module-decimal
one_time = models.TextField(default="client didn't provide information, or want constant supply")
constant_need = models.TextField(default="no information")
how_soon = models.TextField(default="no information")
Expected behaviour: It should look like this:
Unfold, like this: ...but, without errors when saved(till now the error appears when the "save" button is pressed):)
Solution
The error was pointed out in the previous replies, quote:
target_price = models.DecimalField(null=False, blank=True, decimal_places=2, max_digits=7)
So, the proper code would be:
from django.db import models
# Create your models here.
class ProductInquiry(models.Model):
email = models.CharField(null=False, blank=False, max_length=120) # max_lenght required
tittle = models.TextField(null=False, blank=False)
quantity = models.DecimalField(
null=False,
blank=False,
decimal_places=0,
max_digits=4,
default=50,
choices=[(i, i) for i in range(50, 9000, 50)]
)
target_price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)
one_time = models.TextField(default="client didn't provide information, or want constant supply")
constant_need = models.TextField(default="no information")
how_soon = models.TextField(default="no information")
I want to add some tips to avoid similar problems in the future. I just found great charts about when to use null
and blank
in every situation. Charts are from the book "Two Scoops of Django" and they solve every typical case:
Answered By - Paweł Pedryc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.