Django: Admin: changing the widget of the field in Admin

I have a model with a boolean value like that:

class TagCat(models.Model):
    by_admin = models.BooleanField(default=True) 

This appears as a checkbox in admin.

  1. How could I use this as a radio button in admin?
  2. Also, how do I make it be always with a certain selected value in admin?
  3. Also, I want the default value to be the opposite, when a non-admin user adds a TagCat. This field should be hidden from him.

Can someone tell me how to do this? Django documentation doesn't seem to go in such details.

Answers


UPDATE 1: Code that gets me done with 1) (don't forget tot pass CHOICES to the BooleanField in the model)

from main.models import TagCat
from django.contrib import admin
from django import forms

class MyTagCatAdminForm(forms.ModelForm):
    class Meta:
        model = TagCat
        widgets = {
          'by_admin':forms.RadioSelect
        }

class TagCatAdmin(admin.ModelAdmin):
    form = MyTagCatAdminForm


admin.site.register(TagCat, TagCatAdmin)

The radio buttons appear ugly and displaced, but at least, they work

2) I solved with following info in MyModel.py:

BYADMIN_CHOICES = (
    (1, "Yes"),
    (0, "No"),
)
class TagCat(models.Model):
    by_admin = models.BooleanField(choices=BYADMIN_CHOICES,default=1)

Need Your Help

Why does this XML query not work?

xml flex actionscript e4x

Heres some code for a test case. I don't understand why the first two queries produce a result but the third one doesn't. Any ideas?

YUI events don't seem to work recursively

javascript javascript-events yui yui3

I ran across a piece of legacy YUI3 plugin code that was trying to inject itself into an event-based flow. Basically, the code would subscribe to an event X and, in the handler, do something like the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.