Hi all,
I’ve reached about double figures on Django projects but still feel like a beginner and need a little guidance on debugging an issue with AbstractUser not saving permissions and Groups in admin.
This project required that I combine several existing apps and decided to use a Custom User primarily so that I could differentiate sales staff (is_sales) in one of the apps. I created a test project single app and it worked fine, the key being not only to have the “is_sales” field but also define Groups to differentiate sales and control permissions especially in admin rights.
This was the first project using Django v4 and I thought I followed the django guidance (https://docs.djangoproject.com/) on creating Custom User from scratch but clearly something went wrong. My code now contains some additional attempts to resolve the isssue like PermissionMixin, adding Manager and Forms but I’m lost how to debug further.
So when I login to admin as superuser or user and try to edit the permissions or apply a Group I see the confirmation “the user ‘username’ was changed successfully” however if I then view that user in admin or in the database I don’t see those changes.
Here’s my code:-
class User(AbstractUser, PermissionsMixin):
is_sales = models.BooleanField(default=False, help_text='select to ...... ')
def __str__(self):
return self.username
class UserAdmin(UserAdmin):
model = User
list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_sales')
filter_horizontal = ('groups', 'user_permissions')
add_form = UserCreationForm
form = UserChangeForm
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('is_sales',)}),
)
add_fieldsets = UserAdmin.add_fieldsets + (
(None, {'fields': ('is_sales',)}),
)
admin.site.register(User, UserAdmin)
class UserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = User
fields = UserCreationForm.Meta.fields + ('is_sales',)
class UserChangeForm(UserChangeForm):
class Meta(UserChangeForm.Meta):
model = User
When I use the shell to try to debug I can see my main User (AB) has permissions and is part of a Group I made test-admin’
from django.contrib.auth import get_user_model
>>> from django.contrib.auth.models import Group
>>> UserModel=get_user_model()
>>> u = UserModel.objects.get(username='AB')
>>> u.get_group_permissions()
{'itasc.change_pairings', 'itasc.change_measurements', 'trials.add_ctgov1', 'mdaily.view_morg', 'itasc.view_devices', 'mdaily.add_mdaily', 'auth.change_user', 'devices.delete_mdevown', 'mdaily.delete_minv', 'devices.delete_msub', 'mdaily.view_mdaily', 'devices.view_morg', 'auth.add_group', 'devices.add_msub', 'trials.delete_contacts', 'admin.view_logentry', 'itasc.delete_measurements', .........................,
'mdaily.view_minv', 'auth.add_user', 'devices.add_morgs', 'trials.change_contacts', 'devices.delete_minv', 'auth.delete_permission', 'itasc.delete_devices', 'auth.view_permission', 'mdaily.change_mdevown', 'auth.delete_user', 'auth.view_group', 'contenttypes.add_contenttype', 'devices.add_morg', 'contenttypes.delete_contenttype', 'sessions.change_session'}
>>>
If I then runserver, enter Admin and try to change AB or another user, say A2, I get success message but I can see in shell and database nothing actually changes :_
>>> u = UserModel.objects.get(username='A2')
>>> u.get_group_permissions()
set()
So, first I’ve lost the plot how I created AB successfully, I guess I fiddled with something that now impacts my ability to change any user’s permissions.
I would appreciate some guidance on what to look for to resolve this, many thanks.