[Django] #36142: django.shortcuts.get_object_or_404 does not support translation for error messages (i18n)

12 views
Skip to first unread message

Django

unread,
Jan 27, 2025, 3:28:53 AMJan 27
to django-...@googlegroups.com
#36142: django.shortcuts.get_object_or_404 does not support translation for error
messages (i18n)
-------------------------------------+-------------------------------------
Reporter: justbackend | Type:
| Cleanup/optimization
Status: new | Component:
| Internationalization
Version: 5.1 | Severity: Normal
Keywords: i18 | Triage Stage:
Internationalization | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
{{{
class WheatProvidingViewSet(viewsets.ModelViewSet):
queryset = WheatProviding.objects.all()
serializer_class = WheatProvidingSerializer

def destroy(self, request, *args, **kwargs):
instance = self.get_object()
}}}


The code above demonstrates a Django REST Framework (DRF) ModelViewSet.
When a non-existent ID is provided, the destroy method raises a 404 Not
Found error. However, the error message is not translated.

The root cause of this issue is that ModelViewSet relies on Django’s
get_object_or_404 function, which does not support internationalization
(i18n) for its error messages.

Could you consider adding support for translations in the
get_object_or_404 function? This enhancement would allow DRF to natively
support localized error messages, improving its usability in multilingual
applications.
--
Ticket URL: <https://code.djangoproject.com/ticket/36142>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 27, 2025, 3:44:29 AMJan 27
to django-...@googlegroups.com
#36142: django.shortcuts.get_object_or_404 does not support translation for error
messages (i18n)
-------------------------------------+-------------------------------------
Reporter: justbackend | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: | Version: 5.2
Internationalization |
Severity: Normal | Resolution:
Keywords: i18 | Triage Stage: Accepted
Internationalization |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* stage: Unreviewed => Accepted
* version: 5.1 => 5.2


Old description:

> {{{
> class WheatProvidingViewSet(viewsets.ModelViewSet):
> queryset = WheatProviding.objects.all()
> serializer_class = WheatProvidingSerializer
>
> def destroy(self, request, *args, **kwargs):
> instance = self.get_object()
> }}}
>

> The code above demonstrates a Django REST Framework (DRF) ModelViewSet.
> When a non-existent ID is provided, the destroy method raises a 404 Not
> Found error. However, the error message is not translated.
>
> The root cause of this issue is that ModelViewSet relies on Django’s
> get_object_or_404 function, which does not support internationalization
> (i18n) for its error messages.
>
> Could you consider adding support for translations in the
> get_object_or_404 function? This enhancement would allow DRF to natively
> support localized error messages, improving its usability in multilingual
> applications.

New description:

{{{
class WheatProvidingViewSet(viewsets.ModelViewSet):
queryset = WheatProviding.objects.all()
serializer_class = WheatProvidingSerializer

def destroy(self, request, *args, **kwargs):
instance = self.get_object()
}}}


The code above demonstrates a Django REST Framework (DRF) ModelViewSet.
When a non-existent ID is provided, the destroy method raises a 404 Not
Found error. However, the error message is not translated.

The root cause of this issue is that ModelViewSet relies on Django’s
get_object_or_404 function, which does not support internationalization
(i18n) for its error messages.

Could you consider adding support for translations in the
get_object_or_404 function? This enhancement would allow DRF to natively
support localized error messages, improving its usability in multilingual
applications.

--
Comment:

I think that localizing that message makes sense as it is potentially
user-facing, as the use case in the ticket description shows.
--
Ticket URL: <https://code.djangoproject.com/ticket/36142#comment:1>

Django

unread,
Feb 4, 2025, 4:00:12 PMFeb 4
to django-...@googlegroups.com
#36142: django.shortcuts.get_object_or_404 does not support translation for error
messages (i18n)
-------------------------------------+-------------------------------------
Reporter: Abror Izzatullaev | Owner: Joel
Type: | Burns
Cleanup/optimization | Status: assigned
Component: | Version: 5.2
Internationalization |
Severity: Normal | Resolution:
Keywords: i18 | Triage Stage: Accepted
Internationalization |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Joel Burns):

* owner: (none) => Joel Burns
* status: new => assigned

Comment:

I would like to give this issue a try.
--
Ticket URL: <https://code.djangoproject.com/ticket/36142#comment:2>

Django

unread,
Feb 13, 2025, 10:19:53 PMFeb 13
to django-...@googlegroups.com
#36142: django.shortcuts.get_object_or_404 does not support translation for error
messages (i18n)
-------------------------------------+-------------------------------------
Reporter: Abror Izzatullaev | Owner: Joel
Type: | Burns
Cleanup/optimization | Status: assigned
Component: | Version: 5.2
Internationalization |
Severity: Normal | Resolution:
Keywords: i18 | Triage Stage: Accepted
Internationalization |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Joel Burns):

Hi, I'm looking for feedback,

So I think the best way to accomplish this would be to first use:

from django.utils.translation import gettext as _

(the text to be translated needs to be marked with _)

from django.http import HttpResponse
from django.utils.translation import gettext as _
def my_view(request):
output = _("Welcome to my site.")
return HttpResponse(output)

the tagged messages are added to a message file with a .po extension

indicate django.middleware.locale.LocaleMiddleware this must be added to
activate translation features

https://docs.djangoproject.com/en/5.1/topics/i18n/translation/

I'm assuming this file is where this needs to be applied,
https://github.com/django/django/blob/stable/5.1.x/django/shortcuts.py#L65

specifically to the get_object_or_404 or get_list_or_404 methods to
translate the output?
--
Ticket URL: <https://code.djangoproject.com/ticket/36142#comment:3>

Django

unread,
Feb 14, 2025, 6:03:52 AMFeb 14
to django-...@googlegroups.com
#36142: django.shortcuts.get_object_or_404 does not support translation for error
messages (i18n)
-------------------------------------+-------------------------------------
Reporter: Abror Izzatullaev | Owner: Joel
Type: | Burns
Cleanup/optimization | Status: assigned
Component: | Version: 5.2
Internationalization |
Severity: Normal | Resolution:
Keywords: i18 | Triage Stage: Accepted
Internationalization |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Claude Paroz):

Hi Joel,

I think the [https://forum.djangoproject.com/ Django forum] would be a
more appropriate place if you need help for writing the patch.
--
Ticket URL: <https://code.djangoproject.com/ticket/36142#comment:4>

Django

unread,
Jul 22, 2025, 9:43:27 AMJul 22
to django-...@googlegroups.com
#36142: django.shortcuts.get_object_or_404 does not support translation for error
messages (i18n)
-------------------------------------+-------------------------------------
Reporter: Abror Izzatullaev | Owner: Joel
Type: | Burns
Cleanup/optimization | Status: assigned
Component: | Version: 5.2
Internationalization |
Severity: Normal | Resolution:
Keywords: i18 | Triage Stage: Accepted
Internationalization |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Ruhm42):

Hello Joel,

Did you make any progress on this topic? I'd be happy to help.
--
Ticket URL: <https://code.djangoproject.com/ticket/36142#comment:5>

Django

unread,
Sep 23, 2025, 7:34:42 PM (2 days ago) Sep 23
to django-...@googlegroups.com
#36142: django.shortcuts.get_object_or_404 does not support translation for error
messages (i18n)
-------------------------------------+-------------------------------------
Reporter: Abror Izzatullaev | Owner: Ruhm
Type: | Status: assigned
Cleanup/optimization |
Component: | Version: 5.2
Internationalization |
Severity: Normal | Resolution:
Keywords: i18 | Triage Stage: Accepted
Internationalization |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* has_patch: 0 => 1
* owner: Joel Burns => Ruhm

Comment:

[https://github.com/django/django/pull/19668 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36142#comment:6>

Django

unread,
Sep 24, 2025, 9:02:26 AM (yesterday) Sep 24
to django-...@googlegroups.com
#36142: django.shortcuts.get_object_or_404 does not support translation for error
messages (i18n)
-------------------------------------+-------------------------------------
Reporter: Abror Izzatullaev | Owner: Ruhm
Type: | Status: assigned
Cleanup/optimization |
Component: | Version: 5.2
Internationalization |
Severity: Normal | Resolution:
Keywords: i18 | Triage Stage: Ready for
Internationalization | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36142#comment:7>
Reply all
Reply to author
Forward
0 new messages