覆盖指定字段的empty_value_display在django admin中不起作用

时间:2018-10-06 07:49:29

标签: django python-3.x django-models django-admin

我编写了一个模型,如下所示:

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)
    title = models.CharField(max_length=3)
    birth_date = models.DateField(blank=True, null=True)

我的模型管理员如下:

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 = '-empty-'

如django 2.1 https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.empty_value_display中所述,我正在尝试执行此操作,但无法正常工作。

它给了我fielderror。甚至我都不明白它的目的。 我的回溯:

Traceback (most recent call last):
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\contrib\admin\options.py", line 703, in get_form
return modelform_factory(self.model, **defaults)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\forms\models.py", line 551, in modelform_factory
return type(form)(class_name, (form,), form_class_attrs)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\forms\models.py", line 266, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (view_birth_date) specified for Author
During handling of the above exception, another exception occurred: 
Traceback (most recent call last):
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\contrib\admin\options.py", line 604, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\contrib\admin\sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\contrib\admin\options.py", line 1639, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\contrib\admin\options.py", line 1525, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\contrib\admin\options.py", line 1554, in _changeform_view
ModelForm = self.get_form(request, obj, change=not add)
File "C:\Users\monikat\Envs\django_training\lib\site-packages\django\contrib\admin\options.py", line 707, in get_form % (e, self.__class__.__name__)
django.core.exceptions.FieldError: Unknown field(s) (view_birth_date) specified for Author. Check fields/fieldsets/exclude attributes of class AuthorAdmin.

有人知道为什么会出现此错误吗?

1 个答案:

答案 0 :(得分:1)

问题在于您已在模型管理类的fields属性中定义了 view_birth_date

由于它不在您的数据库模型中,因此出现错误。这样的自定义字段将适用于list_display,您可以在其中像上面一样从方法中获取值,请参见docs

现在,如果要在添加/更改页面上显示一些自定义字段,则必须创建一个自定义表单,然后可以在其中定义自定义字段。 如果自定义字段仅用于表示,则可以将其定义为readonly:

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

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

    view_birth_date.empty_value_display = '-empty-'

希望有帮助!