IntegrityError - NOT NULL 约束在 `models.DecimalField` 中失败
IntegrityError - NOT NULL constraint failed in the `models.DecimalField`
我正在尝试构建一个 models.DecimalField
,它将设置为 50 并且会有一个范围(从 50 9000),步长为 50。我读过我可以使用 choices
: "A sequence consisting itself of iterables of exactly two items (e.g. [(A, B), (A, B) ...]) to use作为此字段的选项。如果给出选项,它们将通过模型验证强制执行,并且默认表单小部件将是一个带有这些选项的 select 框,而不是标准文本字段。“.
我不知道如何修复它。肯定错误在 quantity = models.DecimalField
中。
我的代码:
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")
错误:
预期行为:
它应该是这样的:
展开,像这样:
...但是,保存时没有错误(直到现在按下“保存”按钮时出现错误):)
错误与 target_price
有关,您的 quantity
字段(目前)没有任何问题。
通过设置 blank=True
,管理面板中不需要它。然而,它是您数据库中的必填字段,请参阅 null=False
。这就是产生错误的原因。
target_price = models.DecimalField(null=False, blank=True, decimal_places=2, max_digits=7)
只需在此字段中添加一些内容即可。
target_price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)
此错误与您的 quantity
字段无关。
只需更改您的 target_price
字段,将其 null
属性设置为 True
,然后,运行 python manage.py makemigrations
和 python manage.py migrate
命令。这将消除您的 NOT NULL 约束失败 错误。
之前的回复指出错误,引用:
target_price = models.DecimalField(null=False, blank=True, decimal_places=2, max_digits=7)
所以,正确的代码是:
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")
我想添加一些提示以避免将来出现类似问题。我刚刚找到了关于在每种情况下何时使用 null
和 blank
的很棒的图表。图表来自 “Two Scoops of Django” 一书,它们解决了每个典型案例:
我正在尝试构建一个 models.DecimalField
,它将设置为 50 并且会有一个范围(从 50 9000),步长为 50。我读过我可以使用 choices
: "A sequence consisting itself of iterables of exactly two items (e.g. [(A, B), (A, B) ...]) to use作为此字段的选项。如果给出选项,它们将通过模型验证强制执行,并且默认表单小部件将是一个带有这些选项的 select 框,而不是标准文本字段。“.
我不知道如何修复它。肯定错误在 quantity = models.DecimalField
中。
我的代码:
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")
错误:
预期行为:
它应该是这样的:
展开,像这样:
错误与 target_price
有关,您的 quantity
字段(目前)没有任何问题。
通过设置 blank=True
,管理面板中不需要它。然而,它是您数据库中的必填字段,请参阅 null=False
。这就是产生错误的原因。
target_price = models.DecimalField(null=False, blank=True, decimal_places=2, max_digits=7)
只需在此字段中添加一些内容即可。
target_price = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)
此错误与您的 quantity
字段无关。
只需更改您的 target_price
字段,将其 null
属性设置为 True
,然后,运行 python manage.py makemigrations
和 python manage.py migrate
命令。这将消除您的 NOT NULL 约束失败 错误。
之前的回复指出错误,引用:
target_price = models.DecimalField(null=False, blank=True, decimal_places=2, max_digits=7)
所以,正确的代码是:
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")
我想添加一些提示以避免将来出现类似问题。我刚刚找到了关于在每种情况下何时使用 null
和 blank
的很棒的图表。图表来自 “Two Scoops of Django” 一书,它们解决了每个典型案例: