인라인을 포함하여 사용자 권한에 따라 ReadOnly보기를 처리하는 일반 클래스를 작성했습니다.)
models.py에서 :
class User(AbstractUser):
...
def is_readonly(self):
if self.is_superuser:
return False
adminGroup = Group.objects.filter(name="admins")
if adminGroup in self.groups.all():
return False
return True
admin.py에서 :
class ReadOnlyAdmin(admin.ModelAdmin):
def __init__(self, *args, **kwargs):
self._init_readonly_fields = self.readonly_fields
for inline in self.inlines:
inline._init_readonly_fields = inline.readonly_fields
super().__init__(*args,**kwargs)
def change_view( self, request, object_id, form_url='', extra_context=None ):
context = extra_context or {}
if request.user.is_readonly():
self.readonly_fields = [ field.name for field in self.model._meta.get_fields() if not field.auto_created ]
for inline in self.inlines:
inline.readonly_fields = [field.name for field in inline.model._meta.get_fields() if not field.auto_created]
self.save_on_top = False
context['show_save'] = False
context['show_save_and_continue'] = False
else:
self.readonly_fields = self._init_readonly_fields
for inline in self.inlines:
inline.readonly_fields = self._init_readonly_fields
return super().change_view(
request, object_id, form_url, context )
def save_model(self, request, obj, form, change):
if request.user.is_readonly():
return False
return super().save_model( request, obj, form, change )
그런 다음 admin.py에서 일반적으로 클래스를 상속 할 수 있습니다.
class ContactAdmin(ReadOnlyAdmin):
list_display = ("name","email","whatever")
readonly_fields = ("updated","created")
inlines = ( PhoneInline, ... )