Answer the question
In order to leave comments, you need to log in
How to solve KeyError error?
After adding the fields line, an error appeared
admin.py
class NewsAdmin(admin.ModelAdmin):
list_display = ('id', 'title', 'category', 'pub_date', 'is_published', 'get_photo')
list_display_links = ('id', 'title')
search_fields = ('id', 'title', 'content')
list_editable = ('is_published', 'category')
list_filter = ('pub_date','is_published',)
fields = ('id', 'title', 'content', 'category', 'pub_date', 'is_published','photo', 'get_photo')
readonly_fields = ('get_photo', 'pub_date')
def get_photo(self, obj):
if obj.photo:
return mark_safe(f'<img src="{obj.photo.url}" width="75px">')
Answer the question
In order to leave comments, you need to log in
readonly_fields = ('id', 'get_photo', 'pub_date')
or replace id with pk
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question