Fixed #29901 -- Allowed overriding an autocomplete/raw_id_fields/radio_fields widget with ModelAdmin.get_formset().
This commit is contained in:
parent
df448bfd02
commit
3d4d0a25b2
|
@ -224,6 +224,7 @@ class BaseModelAdmin(metaclass=forms.MediaDefiningClass):
|
|||
"""
|
||||
db = kwargs.get('using')
|
||||
|
||||
if 'widget' not in kwargs:
|
||||
if db_field.name in self.get_autocomplete_fields(request):
|
||||
kwargs['widget'] = AutocompleteSelect(db_field.remote_field, self.admin_site, using=db)
|
||||
elif db_field.name in self.raw_id_fields:
|
||||
|
|
|
@ -437,6 +437,28 @@ class ModelAdminTests(TestCase):
|
|||
['main_band', 'day', 'transport', 'id', 'DELETE']
|
||||
)
|
||||
|
||||
def test_raw_id_fields_widget_override(self):
|
||||
"""
|
||||
The autocomplete_fields, raw_id_fields, and radio_fields widgets may
|
||||
overridden by specifying a widget in get_formset().
|
||||
"""
|
||||
class ConcertInline(TabularInline):
|
||||
model = Concert
|
||||
fk_name = 'main_band'
|
||||
raw_id_fields = ('opening_band',)
|
||||
|
||||
def get_formset(self, request, obj=None, **kwargs):
|
||||
kwargs['widgets'] = {'opening_band': Select}
|
||||
return super().get_formset(request, obj, **kwargs)
|
||||
|
||||
class BandAdmin(ModelAdmin):
|
||||
inlines = [ConcertInline]
|
||||
|
||||
ma = BandAdmin(Band, self.site)
|
||||
band_widget = list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields['opening_band'].widget
|
||||
# Without the override this would be ForeignKeyRawIdWidget.
|
||||
self.assertIsInstance(band_widget, Select)
|
||||
|
||||
def test_queryset_override(self):
|
||||
# If the queryset of a ModelChoiceField in a custom form is overridden,
|
||||
# RelatedFieldWidgetWrapper doesn't mess that up.
|
||||
|
|
Loading…
Reference in New Issue