Disable on/off icon for boolean field in Django Disable on/off icon for boolean field in Django django django

Disable on/off icon for boolean field in Django


There is no dirtiness in using this code admin.py:

from mysite.models import Testfrom django.contrib import adminclass TestAdmin(admin.ModelAdmin):    list_display = ('is_blocked_col',)    def is_blocked_col(self, obj):        return not obj.is_blocked # invert the boolean value    is_blocked_col.boolean = True    is_blocked_col.admin_order_field = 'is_blocked'    is_blocked_col.short_description  = 'Is Blocked'admin.site.register(Test, TestAdmin)

If you use this method it will still show the on/off icon. If is_blocked=True then return not obj.is_blocked is going to return False which shown as red icon as desired by you.

EDIT

If you want to use the words True/False instead of the red/green icons you can set

is_blocked_col.boolean = False

in the above code.


I examined the corresponding django code and unfortunately this behaviour is hard-coded so the only solution is the one mentioned in the question:

is_blocked = BooleanField(default=False)def is_blocked_col(self):    return self.is_blockedis_blocked_col.short_description = \is_blocked_col.admin_order_field = 'is_blocked'

Which is much less readable than something like

is_blocked = BooleanField(default=False)is_blocked.boolean = False

(which doesn't work)

or than forcing admin widget to force_unicode or something (which I am not sure how to implement)


I think you can handle it by javascript and css to design you template.

you can see these link : On/Off Button , IOS Buttons , Switch Buttons , CSS3 Buttns

I think it is too easy to use any of them

GOOD LUCKREGARDS

Mohammad