简体   繁体   中英

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.

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. I'm then overriding the following:

The get_form method to return a different form depending on the user. 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.

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:

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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