Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 270
sandeep Avatar asked Nov 30 '12 06:11

sandeep


People also ask

What is FloatField in Django?

FloatField is a floating-point number represented in Python by a float instance. This field is generally used to store huge floating point numbers in the database. The default form widget for this field is a NumberInput when localize is False or TextInput otherwise.

What is default in Django model?

default: The default value for the field. This can be a value or a callable object, in which case the object will be called every time a new record is created. null: If True , Django will store blank values as NULL in the database for fields where this is appropriate (a CharField will instead store an empty string).

What is the default primary key in Django?

Primary Keys By default, Django adds an id field to each model, which is used as the primary key for that model. You can create your own primary key field by adding the keyword arg primary_key=True to a field. If you add your own primary key field, the automatic one will not be added.

What is PK value in Django?

pk is short for primary key, which is a unique identifier for each record in a database. Every Django model has a field which serves as its primary key, and whatever other name it has, it can also be referred to as "pk".


1 Answers

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)
like image 128
Nostalg.io Avatar answered Oct 13 '22 00:10

Nostalg.io