If editable or leadtime not specified when creating a new instance of a model containing them, would expect that ORM would not throw error, would save instance, and that the value of editable would be set as True and leadtime as 0 at the database level.
Thus when executing the following to create a new Product instance, with both editable and leadtime as fields:
django.core.exceptions.ValidationError:
{'editable': ['β<django.db.models.expressions.DatabaseDefault object
at 0x151db1c70>β value must be either True or False.'],
'leadtime': ['β<django.db.models.expressions.DatabaseDefault object
at 0x14e22bad0>β value must be an integer.'}
and similar behavior occurs throughout my codebase for any field for which db_default is set .
Clearly Iβm misunderstanding how db_default should be used. (Develop with Django daily for 6 years, so this is frustrating.) Does anyone have feedback on what Iβm doing wrong?
And, when you call save on the instance, the instance is updated with the db_default values applied.
Note that calling save on an instance does not call any of the clean methods, so the error does not get thrown if you create the instance and save it.
<opinion>
I think you could handle this a couple of different ways.
You could add the fields having db_default attributes to the exclude parameter of full_clean call or create a custom clean method for that field to check the value (if provided) or to allow for a null value. </opinion>
Have submitted a ticket for consideration, suggesting either excluding db_default fields from full_clean() or to incorporate the workarounds @KenWhitesell suggested into the documentation ββ I donβt think Iβm the only person who will expect that the db_default will work the way I did
Thank you @charettes for your discussion in the ticket, you expressed the issue better than I did.
For anyone reading who would like to follow progress: the ticket was accepted, look forward to the change whenever it rolls out: https://code.djangoproject.com/ticket/35223