Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin: readonly_fields not showing on admin. interface

Tags:

django

This should be a pretty straight-forward thing, but it's not working as it should.

model

class Lesson(models.Model):
    lesson_name = models.CharField(max_length=150, primary_key=True)
    json_name = models.CharField(max_length=150, default="", null=True)
    activity_type = models.CharField(max_length=20)
    learning_language = models.CharField(max_length=50)
    known_language = models.CharField(max_length=50)

admin

from .models import Lesson

class LessonAdmin(admin.ModelAdmin):
    fields = ['lesson_name']
    readonly_fields = ['json_name', 'activity_type', 'learning_language', 'known_language']

admin.site.register(Lesson, LessonAdmin) 

But, when I log into the admin site, the fields specified in readonly_fields don't show up at all. I've also tried including the field names in a tuple instead of a list (as the documentation specifies), but that gives me an error even.

like image 255
AmagicalFishy Avatar asked Sep 09 '26 05:09

AmagicalFishy


1 Answers

The actual fields displayed are only set in list_display, fields and fieldsets. readonly_fields just tells the admin, of those that it would display, which to display as read-only. So just add the same fields to your fields list as well. It's duplication, which is annoying, but more explicit.

class LessonAdmin(admin.ModelAdmin):
    fields = ['lesson_name', 'json_name', 'activity_type', 'learning_language', 'known_language']
    readonly_fields = ['json_name', 'activity_type', 'learning_language', 'known_language']

And it doesn't matter if you use a tuple or a list, the django admin accepts both.

like image 162
Niicodemus Avatar answered Sep 10 '26 18:09

Niicodemus