Siva Arunachalam
Siva Arunachalam

Reputation: 7740

Django - ChoiceField - Option Buttons instead of Select box

Is it possible to display the option buttons instead of select box (in the admin interface) for ChoiceField? Any suggestions?

Upvotes: 4

Views: 2498

Answers (3)

jbaums
jbaums

Reputation: 27388

The following ModelAdmin subclass (in your admin.py) does what you're after:

class PersonAdmin(admin.ModelAdmin):
    radio_fields = {"group": admin.VERTICAL}

HORIZONTAL is also possible.

From the Django docs.

Upvotes: 6

dr jimbob
dr jimbob

Reputation: 17711

Yes.

In your admin.py create a ModelAdmin class:

from django.contrib import admin
from django.forms.widgets import RadioSelect ## originally had mistake of django.forms.extras.widgets

class SomeModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
         models.ChoiceField : dict(widget = RadioSelect) 
     }

admin.site.register(SomeModel, SomeModelAdmin)

I'm not sure what you mean by "option buttons" rather than select box, but this is how you change it. You can find the right widget here: http://docs.djangoproject.com/en/dev/ref/forms/widgets/

Upvotes: 1

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56390

There's a snippet on djangosnippets that seems to do something like this for forms given a list of choices, but it doesn't seem to specifically do it for the admin app. You might be able to either leverage this or the ideas within it to get you the rest of the way though.

Upvotes: 1

Related Questions