restrict user access to modeladmin via metaclass

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class user_lock(type):
    @staticmethod
    def queryset(self, request):
        qs = super(self.__class__, self).queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(**{self.__target__:request.user})
    def __new__(self, name, bases, dic):
        dic['queryset'] = self.queryset
        return type(name, bases, dic)

class MyAdmin (ModelAdmin):
    __metaclass__ = user_lock
    __target__ = 'user'

More like this

  1. IsNullFieldListFilter by ssokolow 10 months, 4 weeks ago
  2. Active User Sorted ModelAdmin by daemondazz 3 years, 10 months ago
  3. Model Locking Mixin & Decorator (MySQL Advisory Locks) by pio 1 year, 12 months ago
  4. i18n base model for translatable content by foxbunny 4 years, 10 months ago
  5. Extended i18n base model by alcinnz 1 month, 1 week ago

Comments

(Forgotten your password?)