我如何定义需要admin-form
和admin-fieldsets
取决于request
对象?例如:
我有一些模型:
# models.py
class Shop(models.Model):
name = models.CharField(...)
description = models.TextField(...)
params = models.SomeField(...)
我的字段user.profile
为role
( administrator
, owner
, seller
) 我想根据user.profile.role
进入管理界面不同的字段集,例如:
# admin.py
class ShopAdmin(admin.ModelAdmin):
def custom_get_fieldset_and_form_method(self, request, *args, **kwargs):
if request.user.profile.role == 'administator':
self.fieldsets = self.get_default_fieldset_method(self, *args, **kwargs)
elif request.user.profile.role == 'owner':
self.fieldsets = [
(
'Owner Panel', {'fields': ('name', 'description')},
),
(
'Hidden fields', {'classes':('collapse',),'fields': (`params`,)},
)]
else:
# in other, i want to hide `params` field from form