Opened 4 years ago

Closed 4 years ago

#31046 closed New feature (fixed)

Support callable values in through_defaults.

Reported by: Baptiste Mispelon Owned by: Baptiste Mispelon
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (4)

comment:1 by Baptiste Mispelon, 4 years ago

PR here (with docs and tests)

comment:2 by Mariusz Felisiak, 4 years ago

Owner: changed from nobody to Baptiste Mispelon
Status: newassigned
Summary: Support callable values in through_defaultsSupport callable values in through_defaults.
Triage Stage: UnreviewedAccepted
Type: Cleanup/optimizationNew feature

comment:3 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 6c0341f1:

Refs #31046 -- Added django.db.models.utils.resolve_callables().

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 26cab4e8:

Fixed #31046 -- Allowed RelatedManager.add()/create()/set() to accept callable values in through_defaults.

Note: See TracTickets for help on using tickets.
Back to Top