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
.
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.
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).
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.
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".
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With