Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

python - Django admin list display with list of callable

I have a django project which create lots of reports, and each user has different permissions to view different report. And now I have a admin page which summarize all the user permissions for all reports, and I did it by sth similar to this:

class userAdmin(admin.ModelAdmin):
    def report1_perm(self, user):
        return user.has_perm('report1')
    def report2_perm(self, user):
        return user.has_perm('report2')
    list_display=['report1_perm', 'report2_perm']

Now instead of having to def a diff method for each report, I want to make callables from the list of reports I have, so sth like this

class userAdmin(admin.ModelAdmin):
    def get_list_display(self, request):
        def make_f(report):
            def report_perm(self, user):
                return user.has_perm(report)
            return report_perm
        list_display = []
        for report in reports:
            list_display.append(make_f(report))
        return list_display

However I am gettingTypeError: report_perm() missing 1 required positional argument: 'user' which I do not know how to fix. I wonder am I on the right path or there is a better solution than this

question from:https://stackoverflow.com/questions/65907562/django-admin-list-display-with-list-of-callable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...