Opened 23 months ago
Closed 23 months ago
#34239 closed New feature (wontfix)
Resolve load type-hinted objects in views
Reported by: | Aldo Mateli | Owned by: | nobody |
---|---|---|---|
Component: | Generic views | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
In this ticket I'd like to propose a new feature which loads models from parameter information. A common pattern in views is to have a primary key or candidate key to locate a specific record via get_object_or_404.
What I'd like to propose is to allow users to type-hint a specific parameter and have django load the object automatically by pk or throw 404 if it doesn't exist.
# url pattern for this view: posts/<post>/ # the argument name matches the url variable. def some_view(request, post: Post): pass
This feature would also work if there are multiple objects being loaded in the same view.
Currently in our repository we do this via a decorator, but I feel that this would be a helpful feature that can be merged into core as it reduces the amount of boilerplate.
This feature is inspired from a similar one in Symfony framework. https://symfony.com/doc/current/doctrine.html#doctrine-entity-value-resolver
Looking forward to your thoughts. I can also supply the implementation for this if we decide it is worth adding.
Change History (3)
comment:1 by , 23 months ago
Description: | modified (diff) |
---|
comment:2 by , 23 months ago
Description: | modified (diff) |
---|
comment:3 by , 23 months ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Thanks for this ticket, it's an interesting idea. However, Django doesn't support type hints (see #29299 and the statement from Technical Board). I would suggest wrapping it into a small 3rd-party package so that people can try it out and then starting a discussion on the DevelopersMailingList, where you'll reach a wider audience see what other think. See also the guidelines with regards to requesting features.