我有一个应用程序来管理我的django web-app中的配置(一些用户可以更改配置)。 现在,我试图仅显示用户可以更改的配置(基于组),而不是我的应用程序中可用的所有配置。
我想更改django admin的以下部分,并仅显示用户可以更改的配置。
用户“Admin”的示例我想显示应用程序中可用的所有配置,但对于用户“User1”(group =“group1”),我想显示少量设置。
有没有方法来覆盖django应用程序的这一部分?
i have an app for manage the configuration in my django web-app (some user can change the configurations). Now i'm trying to show only the configuration that the user can change (based on groups) and not all the configuration available in my app.
I want to change the following part of the django admin and show only the configuration that the user can change.
Example for the user "Admin" i want to show all the configuration available in the app, but for the user "User1" (group = "group1") i want to show few setting.
Is there any method to override this part of the django app?
最满意答案
您需要在问题模型groups = models.ManyToManyField(Group)使用groups字段,然后您可以在ModelAdmin中覆盖get_serch_results:
例:
class QuestionAdmin(admin.ModelAdmin): list_display = ('question','otherfield') def get_search_results(self, request, queryset, search_term): queryset, use_distinct = super().get_search_results(request, queryset, search_term) filtered_by_group = queryset.filter(groups__in=request.user.groups.all()) return filtered_by_group, use_distinct admin.site.register(Question, QuestionAdmin)You will need a groups field in your question model groups = models.ManyToManyField(Group) and then you can override get_serch_results in ModelAdmin:
Example:
class QuestionAdmin(admin.ModelAdmin): list_display = ('question','otherfield') def get_search_results(self, request, queryset, search_term): queryset, use_distinct = super().get_search_results(request, queryset, search_term) filtered_by_group = queryset.filter(groups__in=request.user.groups.all()) return filtered_by_group, use_distinct admin.site.register(Question, QuestionAdmin)更多推荐
发布评论