简体   繁体   English

如何在django模型中为FloatField设置默认值

[英]How to set default value for FloatField in django model

How can I set a default value for FloatField in django. 如何在django中为FloatField设置默认值。

Initially when I had created the model and declared the float field as: 最初,当我创建模型并将float字段声明为:

cost = models.FloatField(null=True, blank=True)

and db also created successfully through south migration. 和db也通过南迁移成功创建。

But now when I am trying to do edit on that field using the html form, and no value is entered in the cost field, in the form its throwing value error . 但是现在当我尝试使用html表单对该字段进行编辑时,并且在成本字段中没有输入任何值,在表单中它的抛出value error Thus I think if I can set a default value for cost field it may fix the problem. 因此,我认为如果我可以为成本字段设置默认值,它可以解决问题。

But I don't know how to set a default value for FloatField in django . 但我不知道如何在djangoFloatField设置默认值。

Your code should work. 你的代码应该有效。 Perhaps something is setting the empty value to a non-null value (like an empty string). 也许某些事情是将空值设置为非空值(如空字符串)。 I'd have to see the full error message. 我必须看到完整的错误消息。

Since this is an old thread, I'm going to post the answer to the question I came here for, "How do you set FloatField default to Null in the database?" 由于这是一个旧线程,我将发布我在这里提出的问题的答案, “如何在数据库中将FloatField默认设置为Null?”

cost = models.FloatField(null=True, blank=True, default=None)

Float Field is quite related to Decimal Field so, probably it should take a default argument. Float Field与Decimal Field非常相关,所以可能它应该采用默认参数。 Try doing this.. 试试这个..

cost = models.FloatField(default=to_some_value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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