zl程序教程

您现在的位置是:首页 >  其他

当前栏目

django admin 根据新增还是编辑来显示 readonly_fields 字段

2023-03-07 09:47:33 时间
class AnchorBindAgentAdmin(admin.ModelAdmin):

    def get_readonly_fields(self, request, obj=None):
        """
        Hook for specifying custom readonly fields.
        """
        # 本想用 path 里的 add 来判断
        print(request.path)
        # 根据 obj 是否为空来判断
        if obj:
            self.readonly_fields = ["agent", "anchor"]
        else:
            self.readonly_fields = []
        return self.readonly_fields