Stack Exchange
Choices lost when overriding Django Select with RadioSelect
Given the django model below, is it possible to automatically update a subset of the fields to be RadioSelect instead of the default Select?
class ExampleModel(models.Model):
    field1_radios = models.CharField(max_length=1, choices=DEFAULT_CHOICES)
    field2_radios = models.CharField(max_length=1, choices=DEFAULT_CHOICES)
    field3_radios = models.CharField(max_length=1, choices=DEFAULT_CHOICES)
    field4_radios = models.CharField(max_length=1, choices=DEFAULT_CHOICES)
    field5 = models.CharField(max_length=50)
    ...

I can achieve this manually with:
class ExampleForm(forms.ModelForm):
    class Meta:
        model = ExampleModel
        widgets = {
            'field1_radios': forms.RadioSelect(),
            'field2_radios': forms.RadioSelect(),
            'field3_radios': forms.RadioSelect(),
            'field4_radios': forms.RadioSelect()}

When I try to automate the creation of the widgets the choices are lost (and the radio buttons are not rendered):
def custom_callback(f, *args, **kwargs):
    if f.name.endswith('_radios'):
        formfield = f.formfield()
        formfield.widget = forms.RadioSelect()
        return formfield
    else:
        return f.formfield()

class ExampleForm(forms.ModelForm):
    formfield_callback = custom_callback
    class Meta:
        model = ExampleModel


ahh, it looks like I was missing the final step of re-specifying the original choices, this is not done automatically:
def custom_callback(f, *args, **kwargs):
    if f.name.endswith('_radios'):
        formfield = f.formfield()
        formfield.widget = forms.RadioSelect(choices=formfield.choices)
        return formfield
    else:
        return f.formfield()


Found a solution via the discussion and answer here (that did not use a callback): http://stackoverflow.com/a/11062798

I haven't seen formfield_callback used very often, and I don't think it's documented. 
Another approach would be to override formfield_for_choice_field.
class MyModelAdmin(admin.ModelAdmin):
    ...

    def formfield_for_choice_field(self, db_field, request, **kwargs):
        """Use a radio select instead of a select box"""
        kwargs['widget'] = forms.RadioSelect
        if 'choices' not in kwargs:
            # this was required to prevent displaying the empty label ---------
            kwargs['choices'] = db_field.get_choices(include_blank=False)
        return super(MyModelAdmin, self).formfield_for_choice_field(db_field, request, **kwargs)

