15

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.

2
  • 3
    Doesn't cost = models.FloatField(null=True, blank=True, default=0.0) work? Commented Nov 30, 2012 at 7:06
  • May we assume that you would like to display something like "None" or "Free" for items that have no cost? I.e. a default of 0.0 will not suffice? Commented Mar 21, 2013 at 1:59

2 Answers 2

21

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)
Sign up to request clarification or add additional context in comments.

Comments

10

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)

1 Comment

If you are giving a default value you don't need to set null and blank attributes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.