Django Admin - 重写get_form后缺少内联表单

时间:2012-09-04 05:03:40

标签: python django django-forms django-admin

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

    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

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

谢谢,

1 个答案:

答案 0 :(得分: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)