我使用AbtractUser来扩展https://wsvincent.com/django-custom-user-model-tutorial/中描述的用户模型
并且由于我希望编外用户能够执行某些受限的管理员活动,因此我遵循了https://tryolabs.com/blog/2012/06/18/django-administration-interface-for-non-staff-users/
它按预期工作
当前我的结果发布在http://oi65.tinypic.com/25kuale.jpg
如图所示,具有“用户模型更改”权限的非员工用户在“半径”应用程序菜单下具有“用户”。 我的问题是: 如何在“顶部菜单”(与“更改密码”相同的位置添加“ MyProfile”,单击该位置将调用用户更改表格,如右图所示)。
在models.py
class RadiusUser(AbstractUser) :
radius_password = models.CharField(max_length=40, verbose_name="Hotspot Password",
help_text='Password for Accessing Our Network/Hotspot',blank=True, null=True)
enable = models.BooleanField(default=False)
gb_all = models.FloatField(default=0.0)
gb_day = models.FloatField(default=0.0)
gb_night = models.FloatField(default=0.0)
expired_date = models.DateTimeField(auto_now_add=True)
is_partner = models.BooleanField(default=False)
can_add_credit = models.BooleanField(default=False)
balance = models.IntegerField(blank=False, default=0)
def __str__(self):
return self.username
在admin.py
class RadiusUserAdmin(UserAdmin):
add_form = RadiusUserCreationForm
form = RadiusUserChangeForm
model = RadiusUser
list_display = ['username']
radius_basic_fieldset = ('Radius Configs', {'classes': ('wide',),
'fields': ('radius_password','gb_all','gb_day','gb_night','balance')})
radius_permission_fieldset = ('Radius Permission', {'classes': ('wide',),
'fields': ('enable','is_partner','can_add_credit')})
def get_queryset(self, request):
print('REQUEST=', str(request))
qs = super().get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(pk=request.user.pk)
def get_fieldsets(self, request,obj):
print('OBJECT',obj)
if not obj:
return self.add_fieldsets
if request.user.is_superuser:
fieldsets = UserAdmin.fieldsets + (self.radius_basic_fieldset, self.radius_permission_fieldset)
else :
fieldsets = ((None,{'fields':('username','password')}),
self.radius_basic_fieldset)
print('FIELDSETS:', fieldsets)
return fieldsets
def get_readonly_fields(self, request,obj):
if request.user.is_superuser:
return (None,)
return ('username',
'enable', 'gb_all', 'gb_day', 'gb_night', 'balance')
admin.site.register(RadiusUser, RadiusUserAdmin)
nostaff_admin_site.register(RadiusUser, RadiusUserAdmin)
在forms.py
class RadiusUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = RadiusUser
fields = ('username', 'password', 'radius_password')
class RadiusUserChangeForm(UserChangeForm):
#disabled_fields=['username','enable','gb_all','gb_day','gb_night','expired_date']
class Meta:
model = RadiusUser
fields = ['username','password','radius_password','enable','gb_all','gb_day','gb_night']
def __init__(self, *args, **kwargs):
super(RadiusUserChangeForm, self).__init__(*args, **kwargs)
在project / urls.py
admin.autodiscover()
urlpatterns = [
path('', nostaff_admin_site.urls),
path('radius/', include('radius.urls')),
path('radius/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
]