In the model I set:
在我设置的模型中:
class Task(models.Model):
EstimateEffort = models.PositiveIntegerField('Estimate hours',max_length=200)
Finished = models.IntegerField('Finished percentage',blank=True)
But in the web page, if I didn't set a value for the Finished
field, it is showing an error This field is required
. I tried null=True
and blank=True
. But none of them worked. So could you please tell me how can I make a field allowed to be empty.
但是在网页中,如果我没有为Finished字段设置值,则显示错误此字段是必需的。我尝试了null = True和空白= True。但它们都没有奏效。那么请你告诉我如何让一个场被允许空。
I have found that there is a attribute empty_strings_allowed
, i set it to True, but still the same, and i subclass the models.IntegerField. It still can not work
我发现有一个属性empty_strings_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
3 个解决方案
#1
4
On a form you could set required=False
on the field:
在表单上,您可以在字段上设置required = False:
Finished = forms.IntegerField(required=False)
Or to avoid redefining the field on a ModelForm,
或者为了避免在ModelForm上重新定义字段,
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
#2
2
Maybe a default value is needed
可能需要默认值
finished = models.IntegerField(default=None,blank=True, null=True)
#3
1
Use
Finished = models.IntegerField('Finished percentage', blank=True, null=True)
Read https://docs.djangoproject.com/en/1.4/ref/models/fields/#blank:
null is purely database-related, whereas blank is validation-related.
null纯粹与数据库相关,而blank与验证相关。
You might have defined the field without null=True
first. Changing that in the code now won't change the initial layout of the database. Use South for database migrations or change the database manually.
您可能先定义了没有null = True的字段。现在在代码中更改它不会更改数据库的初始布局。使用South进行数据库迁移或手动更改数据库。
#1
4
On a form you could set required=False
on the field:
在表单上,您可以在字段上设置required = False:
Finished = forms.IntegerField(required=False)
Or to avoid redefining the field on a ModelForm,
或者为了避免在ModelForm上重新定义字段,
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
#2
2
Maybe a default value is needed
可能需要默认值
finished = models.IntegerField(default=None,blank=True, null=True)
#3
1
Use
Finished = models.IntegerField('Finished percentage', blank=True, null=True)
Read https://docs.djangoproject.com/en/1.4/ref/models/fields/#blank:
null is purely database-related, whereas blank is validation-related.
null纯粹与数据库相关,而blank与验证相关。
You might have defined the field without null=True
first. Changing that in the code now won't change the initial layout of the database. Use South for database migrations or change the database manually.
您可能先定义了没有null = True的字段。现在在代码中更改它不会更改数据库的初始布局。使用South进行数据库迁移或手动更改数据库。