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.
- How could I use this as a radio button in admin?
- Also, how do I make it be always with a certain selected value in admin?
- 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.
There is another way to do this that is, IMO much easier if you want every field of the same type to have the same widget. This is done by specifying a formfield_overrides to the ModelAdmin. For example:
More in the docs: https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides
UPDATED: Link to Django 2.0 version: https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides
UPDATE 1: Code that gets me done with 1) (don't forget tot pass CHOICES to the BooleanField in the model)
The radio buttons appear ugly and displaced, but at least, they work
2) I solved with following info in MyModel.py:
Here is a more dynamic extension of mgPePe's response:
This way you get full control over the fields.