Django admin - list_editable - 外键属性?

时间:2017-09-02 12:54:07

标签: python django django-admin django-1.10

是否可以在更改列表中创建可编辑的ForeignKey属性?

每个用户都有UserProfile个对象(OneToOneField)。我希望管理员能够从UserProfile更改列表修改User到期日期。

我可以在更改列表中显示expiry

class UserCustomAdmin(UserAdmin):
    list_display = ['id', 'username','email', 'last_login','userprofile__expiracia']
    # list_editable = ['userprofile__expiracia']
    exclude = ['groups','user_permissions']
    inlines = [UserProfileInline]
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       )}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )

    def userprofile__expiracia(self,obj):
        return obj.userprofile.expiracia

但我无法将userprofile__expiracia添加到list_editable列表中。它提出了:

<class 'dashboard.admin.UserCustomAdmin'>: (admin.E121) The value of 'list_editable[0]' refers to 'userprofile__expiracia', which is not an attribute of 'auth.User'.

我怎样才能让它发挥作用?

0 个答案:

没有答案