简体   繁体   中英

How to set default value for FloatField in django model

How can I set a default value for FloatField in django.

Initially when I had created the model and declared the float field as:

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

and db also created successfully through south migration.

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 . 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 .

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?"

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. Try doing this..

cost = models.FloatField(default=to_some_value)

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