1

如果登录的用户不是超级用户,我需要在 Django Admin 上的表单中隐藏一些字段。这就是我想出的:

    def get_form(self, request, obj=None, **kwargs):
        form = super(RestauranteAdmin, self).get_form(request, obj, **kwargs)
        if not request.user.is_superuser:
            form.base_fields.pop('approved')
            form.base_fields.pop('day_of_the_week')
            form.base_fields.pop('photo')
        form.label_suffix = ''
        return form

这确实隐藏了必要的字段,但由于某种原因,如果用户不是超级用户,管理员也会停止呈现我的内联。据我所知,这种方法应该与内联无关。我错过了什么吗?

谢谢,

4

1 回答 1

4

只需在调用父方法之前将其排除

希望能帮助到你

def get_form(self, request, obj=None, **kwargs):
    if not request.user.is_superuser:              
       self.exclude = []
    else:    
       self.exclude = ['approved','day_of_the_week','photo']

    return super(RestauranteAdmin, self).get_form(request, obj, **kwargs)
于 2012-11-01T04:02:15.250 回答