简体   繁体   中英

IntegrityError - NOT NULL constraint failed in the `models.DecimalField`

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 (eg [(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")

Error: NOT NULL 约束失败

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):)

The error is about the target_price , there is nothing wrong with your quantity field (right now).

By setting the blank=True , it is not required in the admin panel. It is however a required field in your database, see null=False . This is what generates the error you have.

target_price  = models.DecimalField(null=False, blank=True, decimal_places=2, max_digits=7)

Just add something to this field and it will work fine.

target_price  = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=7)

This error is not about your quantity field. Just change your target_price field, set its null attribute to True , After that, run python manage.py makemigrations and python manage.py migrate command. this will remove your NOT NULL constraint failed error.

The answer is already made, but 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" :

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM