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).
--
Ticket URL: <https://code.djangoproject.com/ticket/31046>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
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>
* 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>
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>
* 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>