Re: [Django] #35731: Extend documentation about db_default and DatabaseDefault

21 views
Skip to first unread message

Django

unread,
Sep 5, 2024, 12:51:17 PM9/5/24
to django-...@googlegroups.com
#35731: Extend documentation about db_default and DatabaseDefault
-------------------------------------+-------------------------------------
Reporter: Kyle Bebak | Owner:
Type: | YashRaj1506
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Kyle Bebak):

Indeed, the way I solved this problem was setting both `default` and
`db_default`. I'm guessing this is a fairly common use case, especially
for `BooleanField`, `IntegerField`, `CharField`/`TextField`, etc, where
you usually don't need a DB `Func` or Python function to set a default
value for the field. E.g. I use Django as the SOT for the DB schema, but I
don't only write to the DB with the Django ORM, so having both `default`
and `db_default` is useful for me.

Also, I understand that the model instance needs a sentinel value so it
knows to set `foo.val` after `foo` has been written to the DB. My proposal
is that the sentinel value not be stored on `foo.val`, but rather in some
"private" attribute not likely to be touched by client code. Before `foo`
is written to the DB, I think `foo.val` should be `None`, not an instance
of `DatabaseDefault` (which can be stored elsewhere on the model
instance).

This probably seems like splitting hairs, and not a good reason for
changing the implementation. I like the sentinel living elsewhere because
it makes the model's type interface simpler.

If `default` is passed to the field, then `val` has a type of `int`. If
it's not, then its type is `int | None`. In my proposal above, passing
`db_default` wouldn't change the type of `foo`, whereas the current
implementation means its type is `int | DatabaseDefault`, `int | None`, or
`int`, depending on what combo of `default` and `db_default` is passed.

Anyway, I think a change in documentation would be a good outcome for this
ticket, and that there should be an example of passing both `default` and
`db_default` in the docs. Thank you for taking the time to look at this =)
--
Ticket URL: <https://code.djangoproject.com/ticket/35731#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 5, 2024, 3:32:13 PM9/5/24
to django-...@googlegroups.com
#35731: Extend documentation about db_default and DatabaseDefault
-------------------------------------+-------------------------------------
Reporter: Kyle Bebak | Owner:
Type: | YashRaj1506
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

> I think foo.val should be None, not an instance of DatabaseDefault
(which can be stored elsewhere on the model instance).

Without getting into all the details there are complications in doing that
as `None` is already used to denote `NULL` and a field with a `db_default`
could also be marked `null=True` and assigned `None` before creation to
take precedence over `db_default`

{{{#!python
class Author(models.Model):
favorite_color = models.CharField(db_default="green", null=True)

author = Author()
assert isinstance(author, DatabaseDefault)
author.favorite_color = None
author.save()
author.refresh_from_db()
assert author.favorite_color is None
}}}


In other words we can't use `None` as a sentinel denote a fallback to
`db_default` as it might be explicitly assigned as a desired value.

> If default is passed to the field, then val has a type of int. If it's
not, then its type is int | None. In my proposal above, passing db_default
wouldn't change the type of foo, whereas the current implementation means
its type is int | DatabaseDefault, int | None, or int, depending on what
combo of default and db_default is passed.

That's a bad assumption unfortunately, every property associated with a
field are `| Expression[F]` where `F` is bound to the field type and
represents its `output_field`. In other words, `IntegerField(null=True)`
results in a descriptor that is `int | None | Expression[IntegerField]`
and not simply `int | None`.
--
Ticket URL: <https://code.djangoproject.com/ticket/35731#comment:5>

Django

unread,
Oct 16, 2024, 5:12:33 AM10/16/24
to django-...@googlegroups.com
#35731: Extend documentation about db_default and DatabaseDefault
-------------------------------------+-------------------------------------
Reporter: Kyle Bebak | Owner:
Type: | YashRaj1506
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by YashRaj1506):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35731#comment:6>

Django

unread,
Oct 16, 2024, 6:00:04 AM10/16/24
to django-...@googlegroups.com
#35731: Extend documentation about db_default and DatabaseDefault
-------------------------------------+-------------------------------------
Reporter: Kyle Bebak | Owner:
Type: | YashRaj1506
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by YashRaj1506):

added a patch: https://github.com/django/django/pull/18682

open to suggestions!
--
Ticket URL: <https://code.djangoproject.com/ticket/35731#comment:7>

Django

unread,
Oct 16, 2024, 9:43:00 AM10/16/24
to django-...@googlegroups.com
#35731: Extend documentation about db_default and DatabaseDefault
-------------------------------------+-------------------------------------
Reporter: Kyle Bebak | Owner:
Type: | YashRaj1506
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35731#comment:8>

Django

unread,
Oct 22, 2024, 9:45:36 AM10/22/24
to django-...@googlegroups.com
#35731: Extend documentation about db_default and DatabaseDefault
-------------------------------------+-------------------------------------
Reporter: Kyle Bebak | Owner:
Type: | YashRaj1506
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by YashRaj1506):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35731#comment:9>

Django

unread,
Oct 23, 2024, 4:41:01 AM10/23/24
to django-...@googlegroups.com
#35731: Extend documentation about db_default and DatabaseDefault
-------------------------------------+-------------------------------------
Reporter: Kyle Bebak | Owner:
Type: | YashRaj1506
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35731#comment:10>

Django

unread,
Oct 23, 2024, 5:48:40 AM10/23/24
to django-...@googlegroups.com
#35731: Extend documentation about db_default and DatabaseDefault
-------------------------------------+-------------------------------------
Reporter: Kyle Bebak | Owner:
Type: | YashRaj1506
Cleanup/optimization | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"35ab2e018214479fa712d73f070198299ef670a1" 35ab2e0]:
{{{#!CommitTicketReference repository=""
revision="35ab2e018214479fa712d73f070198299ef670a1"
Fixed #35731 -- Extended db_default docs.

This added a missing db_default reference in docs/topics/db/models.txt,
and added a reference to the DatabaseDefault object.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35731#comment:11>

Django

unread,
Oct 23, 2024, 5:51:47 AM10/23/24
to django-...@googlegroups.com
#35731: Extend documentation about db_default and DatabaseDefault
-------------------------------------+-------------------------------------
Reporter: Kyle Bebak | Owner:
Type: | YashRaj1506
Cleanup/optimization | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"630c9e1f9d05e9d25908a0c5d5f81f1b0b537af0" 630c9e1]:
{{{#!CommitTicketReference repository=""
revision="630c9e1f9d05e9d25908a0c5d5f81f1b0b537af0"
[5.1.x] Fixed #35731 -- Extended db_default docs.

This added a missing db_default reference in docs/topics/db/models.txt,
and added a reference to the DatabaseDefault object.

Backport of 35ab2e018214479fa712d73f070198299ef670a1 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35731#comment:12>
Reply all
Reply to author
Forward
0 new messages