Use case: Want to store 0.0
as default value while no value is passed for a pewee
's DoubleField
. I wrote following code but didn't work for me.
class MyRelation(peewee.Model):
id = peewee.PrimaryKeyField()
percentage = peewee.DoubleField(default=0.0)
Here is an api
@api_blueprint.route('/add_data', methods=['POST'])
@http_header
def add_data():
try:
incoming = json.loads(request.data)
data = MyRelation(percentage=incoming["percentage"])
data.save()
return success_response(201,"Data has been inserted :)")
except Exception as e:
log(str(e))
return raise_error(500, str(e))
which logs following error
10-05-17 05:24:51 Line:199 Message: Error in add_data(),views.api: (1048, "Column 'percentage' cannot be null")
The difference is only enforced on the Python side -- so if you're expecting the server to enforce the default you'll need to use a constraint:
percentage = peewee.DoubleField(constraints=[SQL('DEFAULT 0.0')])
Docs: http://docs.peewee-orm.com/en/latest/peewee/models.html#default-field-values