Django admin: How to display a field that is marked as editable=False' in the model? Django admin: How to display a field that is marked as editable=False' in the model? django django

Django admin: How to display a field that is marked as editable=False' in the model?


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

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


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.pyclass 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.pyclass CustomFooForm(forms.ModelForm):    first = forms.CharField()    class Meta:        model = FooModel        fields = ('second',)class FooAdmin(admin.ModelAdmin):    form = CustomFooFormadmin.site.register(FooModel, FooAdmin)


Your read-only fields must be in fields also:

fields = ['title', 'author', 'content', 'published_date', 'updated_date', 'created_date']readonly_fields = ('published_date', 'updated_date', 'created_date')