Home > Back-end >  Error: (admin.E035) The value of 'readonly_fields[0]' is not a callable
Error: (admin.E035) The value of 'readonly_fields[0]' is not a callable

Time:05-03

Summarize the problem

<class 'news.admin.NewsAdmin'>: (admin.E035) The value of 'readonly_fields[0]' is not a callable, an attribut
e of 'NewsAdmin', or an attribute of 'news.News'.

This all code error. I learn Django, and write code how in author. But write this error :(

Describe what you`ve tried

I immediately insert problem in search, but not found decision

  1. I read this question, but it not finished
  2. This question elementary, author question not correct write variable

Show some code

admin.py

from django.contrib import admin
from .models import News, Category


class NewsAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'category', 'create_at', 'updated_at', 'is_published')
    list_display_links = ('id', 'title',)  # what be links in model
    search_fields = ('title', 'content',)
    list_editable = ('is_published',)  # redact is published in menu
    list_filter = ('is_published', 'category',)  # create table, what in be setting sorted
    fields = ('title', 'category', 'content', 'photo', 'get_photo', 'is_published', 'views',
              'create_at', 'updated_at')
    readonly_fields = ('get_photo', 'views', 'create_at', 'updated_at')


class CategoryAdmin(admin.ModelAdmin):
    list_display = ('id', 'title')  # displays
    list_display_links = ('id', 'title')  # links
    search_fields = ('title',)


admin.site.register(News, NewsAdmin)
admin.site.register(Category, CategoryAdmin)

models.py

from django.db import models
from django.urls import reverse


class News(models.Model):
    title = models.CharField(max_length=150)
    content = models.TextField(blank=True)
    create_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    photo = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    is_published = models.BooleanField(default=True)
    category = models.ForeignKey('Category', on_delete=models.PROTECT)
    views = models.IntegerField(default=0)

    def get_absolute_url(self):
        return reverse('view_news', kwargs={"news_id": self.pk})

    def __str__(self):  # return number position model
        return self.title

    objects = models.Manager()

    class Meta:
        verbose_name = 'NEWS'
        verbose_name_plural = 'NEWS'
        ordering = ['-create_at']


class Category(models.Model):
    title = models.CharField(max_length=150, db_index=True, verbose_name='Title category')

    def get_absolute_url(self):
        return reverse('category', kwargs={"category_id": self.pk})

    class Meta:
        verbose_name = 'Category'
        verbose_name_plural = 'Categories'
        ordering = ['title']

    def __str__(self):  # String views. If not be this parameters, be number vice world
        return self.title

CodePudding user response:

Since you've put 'get_photo' in NewsAdmin.read_only_fields and it is not a model field, it's supposed to be a method or a property in either News or NewsAdmin.

You should change it to 'photo' because read_only_fields only meant for fields to be non-editable.

class NewsAdmin(admin.ModelAdmin):
    ...
    readonly_fields = ('photo', 'views', 'create_at', 'updated_at')
  • Related