简体   繁体   English

在Django admin中使用只读字段时,为什么会出现“ TypeError:元定义类不可迭代”的问题?

[英]Why do I get “TypeError: Meta defining class is not iterable” When using a readonly field in Django admin?

I defined my model as following: 我将模型定义如下:

from django.db import models

class Books(models.Model):
    name = models.CharField(max_length=100)
    author = models.CharField(max_length=100)

    def __str__(self):
        return str(self.name)

The problem is that when I want to make the author field readonly in admin.py as following: 问题是,当我想在admin.py author字段admin.py readonly时,如下所示:

from django.contrib import admin
from core.models import Books

class Books(admin.ModelAdmin):
    readonly_fields=('author',)

admin.site.register(Books)

I get the following error upon running server: 运行服务器时出现以下错误: 在此处输入图片说明

Use a different name for model class and model admin class 模型类模型管理类使用不同的名称

class BooksAdmin(admin.ModelAdmin):  # change the model admin class name
    readonly_fields = ('author',)


admin.site.register(Books, BooksAdmin)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django - 使用 get_readonly_fields 但在添加新记录时获取“NoneType”对象不可迭代 - Django - Using get_readonly_fields but getting 'NoneType' object is not iterable' when adding new record 当我使用 Django 访问管理面板中的图像数据库时,为什么会出现“'图像'object 没有属性'名称'”错误? - Why do I get "'Image' object has no attribute 'name'" error when I visit Images database in admin panel, using Django? Django Forms - 声明字段元 class 不可迭代 - Django Forms - Declarative Fields Meta class is not iterable Django:使用`._meta.get_fields()`时如何排除相关字段? - Django: How do I exclude related fields when using `._meta.get_fields()`? 当我在 django admin 上为模型更改字段时如何做某事 - How do something when i change a field on the django admin for a model 使django只读字段在管理员上可见 - Make a django readonly field visible on the admin 在django admin fieldset readonly字段中隐藏标签 - hide label in django admin fieldset readonly field 如何使 Django 管理员只读 textarea 字段 - How to make a Django admin readonly textarea field 在 Django Admin 中将预填充的 slug 字段设置为只读 - Make the prepopulated slug field as readonly in Django Admin 在django-admin中允许标签为只读字段 - Allowing tags in django-admin for a readonly field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM