Django Admin:动态删除内联字段

时间:2017-09-15 23:00:22

标签: django django-forms django-admin

如果用户没有指定的编辑该数据的权限,我正在尝试动态删除内联。但是,当我以超级用户身份登录时,内联仅显示。我该如何解决这个问题?我正在使用django-nested-inline package.目前正在做的事情:

class ClientAdmin(NestedModelAdmin):
    """
    Admin for Clients. adp_id is only editable if the user has the permission.
    """
    fieldsets = [
    (None, {'fields': [('user', 'company', 'external_id',)]}),
    ]
    inlines = [EventInline]

    def get_form(self, request, obj=None, **kwargs):
        """
        Removes the EventInline if user doesn't have the edit_event permission.
        """
        if not request.user.has_perm('app.edit_event'):
            self.inlines = []
        return super(ClientAdmin, self).get_form(request, obj, **kwargs)


class EventInline(NestedStackedInline):
    model = Event
    fk_name = 'client'
    ordering = ['completion_time']
    fieldsets = [
        (None, {'fields': [('training', 'status', 'due_date'), ('authorized_by', 'completion_time'), 'comments']})
    ]
    extra = 0

我想从EventInline中移除inlines或将EventInline中的所有字段设为只读。有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

原来它没有显示,因为用户没有添加/编辑/删除嵌套内联事件的权限。如果以后有人遇到这个问题,我会把它留下来。

app | event | Can add event
app | event | Can change event
app | event | Can delete event