简体   繁体   English

在Django管理员中使用Django站点框架

[英]Using the Django sites framework in the Django admin

I'm implementing a solution using the Django sites framework for the first time, and am not sure whether there is a better way of implementing it on the Django admin. 我是第一次使用Django站点框架实现解决方案,并且不确定是否有更好的方法在Django管理员上实现它。

Currently I have it working on the frontend, but I want users to be restricted to only manage the content on the backend that belongs to 'their' site (each user is assigned to a site). 目前,我在前端上使用它,但是我希望用户被限制为仅管理属于“其”网站的后端上的内容(每个用户都分配到一个网站)。

To do this currently, I'm splitting the fields available to a superuser (is_superuser) and anyone else by specifying the respective fields in the Admin class. 当前要执行此操作,我通过在Admin类中指定相应的字段来拆分对超级用户(is_superuser)和其他任何人可用的字段。 I'm then overriding the following: 然后,我将覆盖以下内容:

The get_form method to return a different form depending on the user. get_form方法根据用户返回不同的表单。 For instance, a superuser can create content for any site, whereas any other user can only create content for their own site. 例如,超级用户可以为任何站点创建内容,而任何其他用户只能为自己的站点创建内容。

def get_form(self, request, obj=None, **kwargs):
    if request.user.is_superuser:
        self.fieldsets = self.user_fieldsets + self.superuser_fieldsets
    else:
        self.fieldsets = self.user_fieldsets

    return super(FaqCategoryAdmin, self).get_form(request, obj, **kwargs)

The get_queryset method, to only show the relevant entries for the site the user has access to. get_queryset方法,仅显示用户有权访问的网站的相关条目。

def get_queryset(self, request):
    qs = super(FaqCategoryAdmin, self).get_queryset(request)
    if request.user.is_superuser:
        return qs
    else:
        return qs.filter(site=settings.SITE_ID)

The save_model to ensure if a non-superuser saves a new entry, that it defaults to their site: save_model可以确保非超级用户是否保存新条目,该条目默认为其站点:

def save_model(self, request, obj, form, change):
    if not request.user.is_superuser:
        obj.site = get_current_site(request)

    obj.save()

This feels incredibly onerous, given how amazingly simple it is to use the sites framework to restrict frontend display of content (using a model manager). 鉴于使用站点框架来限制内容的前端显示(使用模型管理器)是如此简单,这让人感到难以置信。 Is there a better way of going about this? 有没有更好的方法来解决这个问题?

Thanks! 谢谢!

Yes, there is. 就在这里。 Create your own custom admin base class. 创建您自己的自定义admin基类。 Derive all other admin classes from that one. 从该类派生所有其他管理类。

class MyAdmin(admin.ModelAdmin):

    def get_form(self, request, obj=None, **kwargs):
        if request.user.is_superuser:
            self.fieldsets = self.user_fieldsets + self.superuser_fieldsets
        else:
            self.fieldsets = self.user_fieldsets

        return super(MyAdmin, self).get_form(request, obj, **kwargs)

    def get_queryset(self, request):
        qs = super(MyAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        else:
            return qs.filter(site=settings.SITE_ID)

    def save_model(self, request, obj, form, change):
        if not request.user.is_superuser:
            obj.site = get_current_site(request)

        obj.save()

And then, 然后,

class FaqCategoryAdmin(MyAdmin): 
   # now this class is dry. Because repetitive code is in parent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM