#35659: Unexpected Behavior(footgun) with db_default on a BooleanField
-------------------------------------+-------------------------------------
Reporter: SleipRecx | Type:
| Uncategorized
Status: new | Component: Database
| layer (models, ORM)
Version: 5.0 | Severity: Normal
Keywords: db_default | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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.
Steps to Reproduce:
{{{
from django.db import models
class MyModel(models.Model):
my_field = models.BooleanField(db_default=False)
my_obj = MyModel()
bool(my_obj.my_field) # True, which is unexpected.
my_obj.save()
bool(my_obj.my_field) # False, as expected.
}}}
I know this can be worked around by adding a default attribute in addition
to db_default, but in my opinion its not clear enough from the
documentation that you have to.
--
Ticket URL: <https://code.djangoproject.com/ticket/35659>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107019127cbcc5c-e1b98335-e60c-41f4-84e4-172e26f43567-000000%40eu-central-1.amazonses.com.