【发布时间】:2020-06-15 07:21:12
【问题描述】:
下面是我在 admin.py 文件中的代码。
class JobAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('title', 'organization',)}
save_as = True
admin.site.register(Job, JobAdmin)
问题 #1:
我正在尝试使用 2 个不同的字段预填充我的 slug。标题字段被正确填充,而组织字段是外键字段,它也被填充但具有整数值。我想让这个字段填充其原始值,即组织模型中的名称字段。为此,我确实尝试如下更改组织字段:
class JobAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('title', 'organization__name',)}
但这给了我一个错误。
ERRORS:
<class 'jobs.admin.JobAdmin'>: (admin.E030) The value of 'prepopulated_fields["slug"][0]' refers to 'organization__name', which is not an attribute of 'jobs.Job'.
System check identified 1 issue (0 silenced).
问题 #2:
save_as = True 不启用“另存为新”按钮。我确实参考了Django Admin Document,但我无法理解还需要做什么才能启用它。
如果有人能帮助我解决这些问题,我将不胜感激。提前感谢您的时间和帮助!
【问题讨论】:
标签: django django-models django-forms django-templates django-admin