Answer the question
In order to leave comments, you need to log in
After I added readonly_fields, the server stopped starting, what should I do?
Good evening!
I'm learning how to build a website with Django. I decided to customize the admin panel a bit. After I wrote the readonly_fields line in the admin.py tab and ran the python manage.py runserver command, I got an error:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Program Files (x86)\Python\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "D:\django sites\testsite\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "D:\django sites\testsite\venv\lib\site-packages\django\core\management\commands\runserver.py", line 11
8, in inner_run
self.check(display_num_errors=True)
File "D:\django sites\testsite\venv\lib\site-packages\django\core\management\base.py", line 442, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
<class 'news.admin.NewsAdmin'>: (admin.E035) The value of 'readonly_fields[1]' is not a callable, an attribute
of 'NewsAdmin', or an attribute of 'news.News'.
System check identified 1 issue (0 silenced).
from django.contrib import admin
from django.utils.safestring import mark_safe
from .models import News, Category
class NewsAdmin(admin.ModelAdmin):
list_display = ('id', 'title', 'category', 'created_at', 'updated_at', 'is_published', 'get_photo')
list_display_links = ('id', 'title')
search_fields = ('title', 'content')
list_editable = ('is_published',)
list_filter = ('is_published', 'category')
fields = ('title', 'category', 'content', 'photo', 'get_photo', 'is_published', 'views', 'created_at', 'updated_at')
readonly_fields = ('get_photo', 'views', 'created_at', 'updated_at')
save_on_top = True
def get_photo(self, obj):
if obj.photo:
return mark_safe(f'<img src="{obj.photo.url}" width="75">')
else:
return '-'
get_photo.short_description = 'Миниатюра'
class CategoryAdmin(admin.ModelAdmin):
list_display = ('id', 'title')
list_display_links = ('id', 'title')
search_fields = ('title',)
admin.site.register(News, NewsAdmin)
admin.site.register(Category, CategoryAdmin)
admin.site.site_title = 'Управление новостями'
admin.site.site_header = 'Управление новостями'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question