Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 此字段是django中必需的错误_Python_Django - Fatal编程技术网

Python 此字段是django中必需的错误

Python 此字段是django中必需的错误,python,django,Python,Django,在我设置的模型中: class Task(models.Model): EstimateEffort = models.PositiveIntegerField('Estimate hours',max_length=200) Finished = models.IntegerField('Finished percentage',blank=True) 但是在网页中,如果我没有为Finished字段设置一个值,它会显示一个错误此字段是必需的。我尝试了null=True和bla

在我设置的模型中:

class Task(models.Model):
    EstimateEffort = models.PositiveIntegerField('Estimate hours',max_length=200)
    Finished = models.IntegerField('Finished percentage',blank=True)
但是在网页中,如果我没有为
Finished
字段设置一个值,它会显示一个错误
此字段是必需的
。我尝试了
null=True
blank=True
。但没有一个成功。那么,你能告诉我怎样才能使一个字段被允许为空吗

我发现有一个属性
empty\u strings\u allowed
,我将其设置为True,但仍然相同,并且我对models.IntegerField进行了子类化。它仍然不能工作

class IntegerNullField(models.IntegerField):
    description = "Stores NULL but returns empty string"
    empty_strings_allowed =True
    log.getlog().debug("asas")
    def to_python(self, value):
        log.getlog().debug("asas")
        # this may be the value right out of the db, or an instance
        if isinstance(value, models.IntegerField):
            # if an instance, return the instance
            return value
        if value == None:
            # if db has NULL (==None in Python), return empty string
            return ""
        try:
            return int(value)
        except (TypeError, ValueError):
            msg = self.error_messages['invalid'] % str(value)
            raise exceptions.ValidationError(msg)

    def get_prep_value(self, value):
        # catches value right before sending to db
        if value == "":
            # if Django tries to save an empty string, send to db None (NULL)
            return None
        else:
            return int(value) # otherwise, just pass the value
使用

阅读:

null纯粹与数据库相关,而blank与验证相关。

您可能在没有
null=True的情况下首先定义了字段。现在在代码中更改它不会更改数据库的初始布局。用于数据库迁移或手动更改数据库。

在可在字段中设置的表单上:

Finished = forms.IntegerField(required=False)
或者为了避免在模型表单上重新定义字段

def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)
    self.fields['Finished'].required = False
    #self.fields['Finished'].empty_label = 'Nothing' #optionally change the name

可能需要一个默认值

finished = models.IntegerField(default=None,blank=True, null=True)

您的表单是什么样子的?您是否执行了“python manage.py syncdb”@Yuji Tomita我使用的是管理员的默认表单,而不是我的自定义表单对不起,这不起作用,即使我将两者都设置为blank=True,null=True。它需要输入一个值我删除数据库文件中的表并再次运行sycndb,它仍然是相同的模型:Finished=models.IntegerField(blank=True,null=True)您已经有表单了吗?因为在表单上指定字段时,默认情况下字段是必需的,所以在模型中使其为空并不重要,因为它在html中表示时是必需的。但不能将表单字段设置为可选字段,也不能将模型上的字段设置为必需字段
finished = models.IntegerField(default=None,blank=True, null=True)