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,15 +224,16 @@ class BaseModelAdmin(metaclass=forms.MediaDefiningClass):
|
||||||
"""
|
"""
|
||||||
db = kwargs.get('using')
|
db = kwargs.get('using')
|
||||||
|
|
||||||
if db_field.name in self.get_autocomplete_fields(request):
|
if 'widget' not in kwargs:
|
||||||
kwargs['widget'] = AutocompleteSelect(db_field.remote_field, self.admin_site, using=db)
|
if db_field.name in self.get_autocomplete_fields(request):
|
||||||
elif db_field.name in self.raw_id_fields:
|
kwargs['widget'] = AutocompleteSelect(db_field.remote_field, self.admin_site, using=db)
|
||||||
kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.remote_field, self.admin_site, using=db)
|
elif db_field.name in self.raw_id_fields:
|
||||||
elif db_field.name in self.radio_fields:
|
kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.remote_field, self.admin_site, using=db)
|
||||||
kwargs['widget'] = widgets.AdminRadioSelect(attrs={
|
elif db_field.name in self.radio_fields:
|
||||||
'class': get_ul_class(self.radio_fields[db_field.name]),
|
kwargs['widget'] = widgets.AdminRadioSelect(attrs={
|
||||||
})
|
'class': get_ul_class(self.radio_fields[db_field.name]),
|
||||||
kwargs['empty_label'] = _('None') if db_field.blank else None
|
})
|
||||||
|
kwargs['empty_label'] = _('None') if db_field.blank else None
|
||||||
|
|
||||||
if 'queryset' not in kwargs:
|
if 'queryset' not in kwargs:
|
||||||
queryset = self.get_field_queryset(db, db_field, request)
|
queryset = self.get_field_queryset(db, db_field, request)
|
||||||
|
|
|
@ -437,6 +437,28 @@ class ModelAdminTests(TestCase):
|
||||||
['main_band', 'day', 'transport', 'id', 'DELETE']
|
['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):
|
def test_queryset_override(self):
|
||||||
# If the queryset of a ModelChoiceField in a custom form is overridden,
|
# If the queryset of a ModelChoiceField in a custom form is overridden,
|
||||||
# RelatedFieldWidgetWrapper doesn't mess that up.
|
# RelatedFieldWidgetWrapper doesn't mess that up.
|
||||||
|
|
Loading…
Reference in New Issue