[Django] #33911: Add hook for BaseModelAdmin.raw_id_fields

9 views
Skip to first unread message

Django

unread,
Aug 9, 2022, 10:56:11 PM8/9/22
to django-...@googlegroups.com
#33911: Add hook for BaseModelAdmin.raw_id_fields
-----------------------------------------+------------------------
Reporter: ibaguio | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: dev
Severity: Normal | Keywords: admin
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+------------------------
`BaseModelAdmin.raw_id_fields` is a very feature in django admin that
prevents page timing out or crashing when a related field's table has too
many records for the default django admin dropdown widget. Alternatively,
`autocomplete_fields` has the same objective, but using a different
widget.

Currently, there exists a method hook
`BaseModelAdmin.get_autocomplete_fields(request)` which can be overridden
by child admin classes for customized logic. But there is no
`get_raw_id_fields` hook that allows us to add some logic for
`raw_id_fields`. It would be useful to have this hook also included in the
admin. Right now we are working around this by adding our custom logic to
populate the raw_id_fields in the `__init__` method instead.

We need this code to be added to the `BaseAdmin` class.

{{{
def get_raw_id_fields(self, request):
"""
Return a list of ForeignKey and/or ManyToMany fields which should use
a raw id widget.
"""
return self.raw_id_fields
}}}

--
The specific use case we have for this is we are writing a custom admin
class that automatically sets all of the related fields (FK, O2O, M2M) of
a model to `raw_id_fields` or `autocomplete_fields` so devs doesn't have
to manually configure the admin every time a new field is added / removed
to the model.

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

Django

unread,
Aug 9, 2022, 10:56:51 PM8/9/22
to django-...@googlegroups.com
#33911: Add hook for BaseModelAdmin.raw_id_fields
-------------------------------+--------------------------------------

Reporter: ibaguio | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:

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

* Attachment "0001-Add-BaseAdmin.get_raw_id_fields-hook.patch" added.

Patch that implements the requested feature

Django

unread,
Aug 10, 2022, 1:38:39 AM8/10/22
to django-...@googlegroups.com
#33911: Add hook for BaseModelAdmin.raw_id_fields
-------------------------------------+-------------------------------------
Reporter: Ivan Dominic Baguio | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: dev
Severity: Normal | Resolution: duplicate

Keywords: admin | 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: => duplicate
* easy: 1 => 0


Comment:

Duplicate of #17881.

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

Reply all
Reply to author
Forward
0 new messages