﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17881	Implement BaseModelAdmin.get_raw_id_fields, similar to get_readonly_fields	Aymeric Augustin	nobody	"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."	New feature	new	contrib.admin	1.4-beta-1	Normal		raw_id_fields	kmike84@… romain.garrigues.cs@… elonzh	Accepted	1	1	1	0	0	0
