[Django] #31046: Support callable values in through_defaults

65 views
Skip to first unread message

Django

unread,
Nov 29, 2019, 12:05:01 PM11/29/19
to django-...@googlegroups.com
#31046: Support callable values in through_defaults
-------------------------------------+-------------------------------------
Reporter: Baptiste | Owner: nobody
Mispelon |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Ticket #9475 gave us `through_defaults` but unlike the `defaults` argument
of `get_or_create` [1] or the `default` argument of any model field, it
doesn't allow callable values.

Callable values are passed through without being evaluated so the exact
behavior depends on the fields. With a `CharField` for example, the
`repr()` of the function will be saved to the database which is most
likely not what the user is expecting.

I took a look at the original ticket and pull request but couldn't find a
mention of this use-case (ctrl+F for the word "callable") so it seems that
it was an oversight rather than a deliberate design decision.

Code-wise, fixing this is actually pretty straightforward and doesn't seem
to cause any regression (see attached pull request).

[1]
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.get_or_create

--
Ticket URL: <https://code.djangoproject.com/ticket/31046>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 29, 2019, 12:06:50 PM11/29/19
to django-...@googlegroups.com
#31046: Support callable values in through_defaults
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Baptiste Mispelon):

[https://github.com/django/django/pull/12161 PR here] (with docs and
tests)

--
Ticket URL: <https://code.djangoproject.com/ticket/31046#comment:1>

Django

unread,
Dec 2, 2019, 3:10:51 AM12/2/19
to django-...@googlegroups.com
#31046: Support callable values in through_defaults.
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Baptiste
| Mispelon
Type: New feature | Status: assigned

Component: Database layer | Version: master
(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 felixxm):

* owner: nobody => Baptiste Mispelon
* status: new => assigned
* stage: Unreviewed => Accepted
* type: Cleanup/optimization => New feature


--
Ticket URL: <https://code.djangoproject.com/ticket/31046#comment:2>

Django

unread,
Dec 3, 2019, 2:08:29 PM12/3/19
to django-...@googlegroups.com
#31046: Support callable values in through_defaults.
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Baptiste
| Mispelon
Type: New feature | Status: assigned
Component: Database layer | Version: master
(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 Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"6c0341f127c4d0772aff8e187e620fcb300fa9a9" 6c0341f1]:
{{{
#!CommitTicketReference repository=""
revision="6c0341f127c4d0772aff8e187e620fcb300fa9a9"
Refs #31046 -- Added django.db.models.utils.resolve_callables().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31046#comment:3>

Django

unread,
Dec 3, 2019, 2:08:30 PM12/3/19
to django-...@googlegroups.com
#31046: Support callable values in through_defaults.
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Baptiste
| Mispelon
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
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 Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"26cab4e8c113b873b99b747128a064ba72d40c74" 26cab4e8]:
{{{
#!CommitTicketReference repository=""
revision="26cab4e8c113b873b99b747128a064ba72d40c74"
Fixed #31046 -- Allowed RelatedManager.add()/create()/set() to accept
callable values in through_defaults.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31046#comment:4>

Reply all
Reply to author
Forward
0 new messages