[Django] #34239: Resolve load type-hinted objects in views

8 views
Skip to first unread message

Django

unread,
Jan 2, 2023, 4:36:32 PM1/2/23
to django-...@googlegroups.com
#34239: Resolve load type-hinted objects in views
-----------------------------------------+------------------------
Reporter: maldoinc | Owner: nobody
Type: New feature | Status: new
Component: Generic views | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
In this ticket I'd like to propose a new 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 or throw 404 if it
doesn't exist.

{{{
#!div style="font-size: 80%"
{{{#!python
# 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.

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

Django

unread,
Jan 2, 2023, 4:37:04 PM1/2/23
to django-...@googlegroups.com
#34239: Resolve load type-hinted objects in views
-------------------------------+--------------------------------------

Reporter: maldoinc | Owner: nobody
Type: New feature | Status: new
Component: Generic views | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by maldoinc:

Old description:

> In this ticket I'd like to propose a new 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 or throw 404 if
> it doesn't exist.
>
> {{{
> #!div style="font-size: 80%"
> {{{#!python
> # 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.

New description:

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 or throw 404 if it
doesn't exist.

{{{
#!div style="font-size: 80%"
{{{#!python
# 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.

--

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

Django

unread,
Jan 2, 2023, 6:32:16 PM1/2/23
to django-...@googlegroups.com
#34239: Resolve load type-hinted objects in views
-------------------------------+--------------------------------------
Reporter: maldoinc | Owner: nobody
Type: New feature | Status: new
Component: Generic views | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by maldoinc:

Old description:

> In this ticket I'd like to propose a new feature which loads models from

New description:

parameter and have django load the object automatically by pk or throw 404


if it doesn't exist.

{{{
#!div style="font-size: 80%"
{{{#!python
# 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.

--

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

Django

unread,
Jan 2, 2023, 11:45:01 PM1/2/23
to django-...@googlegroups.com
#34239: Resolve load type-hinted objects in views
-------------------------------+--------------------------------------
Reporter: Aldo Mateli | Owner: nobody
Type: New feature | Status: closed

Component: Generic views | Version: dev
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => wontfix


Comment:

Thanks for this ticket, it's an interesting idea. However, Django doesn't
support type hints (see #29299 and the [https://groups.google.com/g
/django-developers/c/C_Phs05kL1Q 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
[https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
features/#requesting-features the guidelines with regards to requesting
features].

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

Reply all
Reply to author
Forward
0 new messages