Re: [Django] #35659: Document db_default behaviour before instances are saved (was: Unexpected Behavior(footgun) with db_default on a BooleanField)

4 views
Skip to first unread message

Django

unread,
Aug 6, 2024, 10:21:14 AM8/6/24
to django-...@googlegroups.com
#35659: Document db_default behaviour before instances are saved
--------------------------------------+------------------------------------
Reporter: Markus Andresen | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 5.0
Severity: Normal | Resolution:
Keywords: db_default | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by David Sanders):

* component: Database layer (models, ORM) => Documentation
* stage: Unreviewed => Accepted
* summary: Unexpected Behavior(footgun) with db_default on a BooleanField
=> Document db_default behaviour before instances are saved
* type: Uncategorized => Cleanup/optimization

Comment:

Thanks for the report, though I disagree with the analysis of it being
unexpected 😊

> There seems to be an unexpected behavior when using the db_default
attribute on a BooleanField. The bool() evaluation when the instance is
not saved to the database does not reflect the specified db_default value.

It's not safe to randomly cast things without at least checking what the
underlying type of the variable is. Eg `bool("false") == True`. In this
case `my_field` is an instance of `DatabaseDefault`.

The documentation of `DatabaseDefault` is missing. The docs could probably
be improved to make mention of it and that field values will be of this
type before saving to the database.

Accepting the ticket as a docs improvement 👍
--
Ticket URL: <https://code.djangoproject.com/ticket/35659#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages