V
V
Vova1357982021-04-27 14:59:43
Django
Vova135798, 2021-04-27 14:59:43

How to solve KeyError error?

After adding the fields line, an error appeared6087fc8141091229048275.png

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

2 answer(s)
M
maksam07, 2021-04-28
@Vova135798

readonly_fields = ('id', 'get_photo', 'pub_date')
or replace id with pk

S
Sergey Tikhonov, 2021-04-27
@tumbler

Remove the id from the list, it still doesn't change.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question