views:

32

answers:

2

Even though a field is marked as 'editable=False' in the model, I would like the admin page to display it. Currently it hides the field altogether.. How can this be achieved ?

+1  A: 

Update

This solution is useful if you want to keep the field editable in Admin but non-editable everywhere else. If you want to keep the field non-editable throughout then @Till Backhaus' answer is the better option.

Original Answer

One way to do this would be to use a custom ModelForm in admin. This form can override the required field to make it editable. Thereby you retain editable=False everywhere else but Admin. For e.g. (tested with Django 1.2.3)

# models.py
class FooModel(models.Model):
    first = models.CharField(max_length = 255, editable = False)
    second  = models.CharField(max_length = 255)

    def __unicode__(self):
        return "{0} {1}".format(self.first, self.second)

# admin.py
class CustomFooForm(forms.ModelForm):
    first = forms.CharField()

    class Meta:
        model = FooModel
        fields = ('second',)

class FooAdmin(admin.ModelAdmin):
    form = CustomFooForm

admin.site.register(FooModel, FooAdmin)
Manoj Govindan
Thanks. Handy example anyway for custom admin model form
bugspy.net
+3  A: 

Use Readonly Fields. Like so (for django >= 1.2):

MyModelAdmin(admin.ModelAdmin):
    readonly_fields=('first',)

Edit: Punctuation

Till Backhaus
+1. Especially if you _don't_ plan to edit the field at all in Admin.
Manoj Govindan