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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With