Django使用每组自定义数据覆盖Admin App(Django Override Admin App with custom data per group)

编程入门 行业动态 更新时间:2024-10-27 08:28:45
Django使用每组自定义数据覆盖Admin App(Django Override Admin App with custom data per group)

我有一个应用程序来管理我的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)

更多推荐

本文发布于:2023-07-27 05:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1285459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   每组   数据   Admin   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!