Django管理列表可编辑外键属性?

2024-10-03 21:28:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我可以在更改列表中显示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列表中。它引发了:

^{pr2}$

我该怎么做?在


Tags: nameobj编辑fields列表isemailusername