This can be done using the choices model field argument choices
myfield = models.CharField(max_length=256, choices=[('green', 'green'), ('red', 'red')]
The only problem is that if you already have a value in the database that does not match one of them, django might just use one of the options by default.
If this is a problem and you want to save these values, I can redefine the administrator form and either supply only ChoiceField in the add operations, or dynamically add everything in the database as one of the valid options.
class MyForm(ModelForm): MY_CHOICES = [('green', 'green'), ('red', 'red')] def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) if self.instance.id: CHOICES_INCLUDING_DB_VALUE = [(self.instance.field,)*2] + self.MY_CHOICES self.fields['my_field'] = forms.ChoiceField( choices=CHOICES_INCLUDING_DB_VALUE) class MyAdmin(admin.ModelAdmin): form = MyForm
Yuji 'Tomita' Tomita
source share