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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…