(Django 3.1.6/Python 3.8.7) 在字段或字段集 admin.py 中使用命名方法不起作用?

问题描述 投票:0回答:2

无论我尝试什么,我都不能像文档中那样使用命名方法作为字段名称。最终我会让该方法检索 ForeignKey 的一个字段,但现在我只想弄清楚这个简化示例中我做错了什么。

在这个来自 docs.djangoproject.com 的示例中 他们使用类方法“view_birth_date”,就像我使用“get_test”(下图)一样,甚至通过我在

fieldsets
而不是
fields
中使用它应该工作。

我用

fields = 
fieldsets = 
试过了,我得到了相同的结果(错误)。无论哪种方式,它都找不到字段“get_test”。我也试过将
def get_test(self)
放在
fieldsets = 
之上,但这没有任何区别。

它应该和文档中的一样工作,但事实并非如此。我做错了什么?

这应该有效:

@admin.register(Feeddata)
class AdminFeeddata(admin.ModelAdmin):
    """Feeddata admin."""

    list_display = ['id', 'eventtime', 'feed']
    fieldsets = [
        ('Meta', {
            'fields': ['feed', 'eventtime', 'get_test']
        }),
        ('JSON Data', {
            'fields': ('json_data', )
        }),
    ]

    # pylint: disable=no-self-use
    def get_test(self):
        """Test string."""
        return 'test'

但是我得到一个错误:

FieldError at /admin/feeder/feeddata/fff1d764-52cd-499f-9ae9-b1251a806b5b/change/
Unknown field(s) (get_test) specified for Feeddata. Check fields/fieldsets/exclude attributes of class AdminFeeddata.
Request Method: GET
Request URL:    http://localhost:8000/admin/feeder/feeddata/fff1d764-52cd-499f-9ae9-b1251a806b5b/change/
Django Version: 3.1.6
Exception Type: FieldError
Exception Value:    
Unknown field(s) (get_test) specified for Feeddata. Check fields/fieldsets/exclude attributes of class AdminFeeddata.
Exception Location: /usr/local/lib/python3.8/site-packages/django/contrib/admin/options.py, line 711, in get_form
Python Executable:  /usr/local/bin/python
Python Version: 3.8.7
Python Path:    
['/code',
 '/code',
 '/usr/local/bin',
 '/usr/local/lib/python38.zip',
 '/usr/local/lib/python3.8',
 '/usr/local/lib/python3.8/lib-dynload',
 '/usr/local/lib/python3.8/site-packages']
Server time:    Thu, 18 Feb 2021 23:44:12 -0800
python django python-3.8
2个回答
0
投票

你忘了在 get_test 方法中添加 obj 参数,它是你的实例模型

@admin.register(Feeddata)
class AdminFeeddata(admin.ModelAdmin):
    """Feeddata admin."""

    list_display = ['id', 'eventtime', 'feed']
    fieldsets = [
        ('Meta', {
            'fields': ['feed', 'eventtime', 'get_test']
        }),
        ('JSON Data', {
            'fields': ('json_data', )
        }),
    ]

    # pylint: disable=no-self-use
    def get_test(self, obj):
        """Test string."""
        return 'test'

这里是文档中的一个例子:

from django.contrib import admin

class AuthorAdmin(admin.ModelAdmin):
    fields = ('name', 'title', 'view_birth_date')

    def view_birth_date(self, obj):
        return obj.birth_date

    view_birth_date.empty_value_display = '???'

0
投票

为了在您的表单字段集中显示自定义字段,这是方法的结果,您应该将其传递给

readonly_fields
。将
get_test
添加到
readonly_fields
列表中,如下所示。

@admin.register(Feeddata)
class AdminFeeddata(admin.ModelAdmin):
    """Feeddata admin."""

    readonly_fields = ["get_test"] #new
    list_display = ['id', 'eventtime', 'feed']
    fieldsets = [
        ('Meta', {
            'fields': ['feed', 'eventtime', 'get_test']
        }),
        ('JSON Data', {
            'fields': ('json_data', )
        }),
    ]

    # pylint: disable=no-self-use
    def get_test(self):
        """Test string."""
        return 'test'
© www.soinside.com 2019 - 2024. All rights reserved.