[Django] #17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields

23 views
Skip to first unread message

Django

unread,
Mar 12, 2012, 2:16:55 PM3/12/12
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-----------------------------------------+----------------------------
Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
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 order to prevent massive drop downs for related fields when the related
model has lots of items, I'd like to add such fields automatically to
`raw_id_fields`.

I've managed to make them read only by implementing the following
`get_readonly_fields` method in the base class that all my `ModelAdmin`
classes inherit:
{{{
MODELS_WITH_TOO_MANY_INSTANCES_FOR_ADMIN_SELECT = [
'auth.User',
# ...
}

class ModelAdmin(admin.ModelAdmin):

def get_readonly_fields(self, request, obj=None):
explicit_readonly_fields = super(ModelAdmin,
self).get_readonly_fields(request, obj)
automatic_readonly_fields =
self.related_fields_with_too_many_instances()
return tuple(set(explicit_readonly_fields) |
set(automatic_readonly_fields))

def related_fields_with_too_many_instances(self):
fields = []
for f in self.model._meta.fields:
# If the field isn't shown anyway, don't bother.
if self.fields and not f.name in self.fields:
continue
# If we made the field editable by id, that means we want to
edit it, even if it's impractical
if f.name in self.raw_id_fields:
continue
# If the field is a ForeignKey to a blacklisted model, make it
read only.
if isinstance(f, (django_models.ForeignKey,
django_models.ManyToManyField)):
related_model = '%s.%s' % (f.rel.to._meta.app_label,
f.rel.to._meta.object_name)
if related_model in
MODELS_WITH_TOO_MANY_INSTANCES_FOR_ADMIN_SELECT:
fields.append(f.name)
return fields

}}}

However, power users occasionally complain that they can no longer edit
these fields... which is why I'd like a `get_raw_id_fields` method that I
could override.

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

Django

unread,
Mar 12, 2012, 2:22:43 PM3/12/12
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-------------------------------+--------------------------------------
Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by aaugustin):

* has_patch: 0 => 1


Comment:

I'm uploading an untested patch showing the idea.

It'd be nice if `get_raw_id_fields` accepted an optional `obj` argument,
but apparently, this information isn't available in the `formfield_for_*`
methods. This needs more investigation.

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

Django

unread,
Mar 12, 2012, 4:21:49 PM3/12/12
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-------------------------------+--------------------------------------
Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by julien):

* needs_docs: 0 => 1
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted


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

Django

unread,
Apr 6, 2012, 12:36:28 AM4/6/12
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-------------------------------+--------------------------------------
Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by kmike):

* cc: kmike84@… (added)


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

Django

unread,
Oct 4, 2014, 10:15:16 PM10/4/14
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-------------------------------+--------------------------------------

Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: raw_id_fields | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by collinanderson):

* keywords: => raw_id_fields


--
Ticket URL: <https://code.djangoproject.com/ticket/17881#comment:4>

Django

unread,
Oct 4, 2014, 10:15:40 PM10/4/14
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-------------------------------+--------------------------------------

Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: raw-id-fields | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by collinanderson):

* keywords: raw_id_fields => raw-id-fields


--
Ticket URL: <https://code.djangoproject.com/ticket/17881#comment:5>

Django

unread,
Oct 4, 2014, 10:19:38 PM10/4/14
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-------------------------------+--------------------------------------

Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: raw_id_fields | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by collinanderson):

* keywords: raw-id-fields => raw_id_fields


--
Ticket URL: <https://code.djangoproject.com/ticket/17881#comment:6>

Django

unread,
Jun 30, 2016, 10:31:48 AM6/30/16
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-------------------------------+--------------------------------------

Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: raw_id_fields | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by romgar):

I was wondering the other day why the default Django admin widget for
related fields is a drop-down by default, instead of an autocomplete (as
we can have with `raw_id_field` option), when my Django admin page was
crashing down.
I understand that it's more convenient with a drop-down when you have few
data, but there is a real risk for Django to crash / expose a lot of data
through this feature, and that's not what we are used to with Django.
Usually, default choices in the whole framework are "safe".
@aaugustin, I guess you are not working on it right now, as this ticket is
4 years old.
Do you think it make sense to change this default behaviour, or it has too
many impacts?

--
Ticket URL: <https://code.djangoproject.com/ticket/17881#comment:7>

Django

unread,
Jun 30, 2016, 11:31:17 AM6/30/16
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-------------------------------+--------------------------------------

Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: raw_id_fields | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by romgar):

* cc: romain.garrigues.cs@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/17881#comment:8>

Django

unread,
Jun 30, 2016, 12:46:23 PM6/30/16
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
-------------------------------+--------------------------------------

Reporter: aaugustin | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: raw_id_fields | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by timgraham):

The ticket for adding a more usable autocomplete widget is #14370. We
might consider changing the default to that widget at some point, but it's
not a decision to make lightly.

Feel free to work on this ticket if you like.

--
Ticket URL: <https://code.djangoproject.com/ticket/17881#comment:9>

Django

unread,
Apr 26, 2017, 8:58:13 AM4/26/17
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
----------------------------------+--------------------------------------
Reporter: Aymeric Augustin | Owner: nobody

Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: raw_id_fields | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by Fabien Schwob):

Is anyone working on this ticket or can I work on it ?

--
Ticket URL: <https://code.djangoproject.com/ticket/17881#comment:10>

Django

unread,
Jan 17, 2022, 3:46:54 AM1/17/22
to django-...@googlegroups.com
#17881: Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields
----------------------------------+--------------------------------------
Reporter: Aymeric Augustin | Owner: nobody

Type: New feature | Status: new
Component: contrib.admin | Version: 1.4-beta-1
Severity: Normal | Resolution:
Keywords: raw_id_fields | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by elonzh):

* cc: elonzh (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/17881#comment:11>

Reply all
Reply to author
Forward
0 new messages