无法从Admin保存Django代理用户模型,请求协助排查错误
Hey there! Let's break down the issue you're facing when saving user objects in Django's admin panel. Based on the snippet of your admin.py code, I can spot a few potential red flags to check first:
1. Missing Quotes in list_display Entries
Looking at your list_display tuple:
list_display = ('username', 'email', 'first_name', 'last_name', 'is_active', roles, login)
Wait a second—roles and login are written without quotes! In Django's list_display, you need to reference fields or method names as strings. Leaving them unquoted will make Python try to resolve them as variables in the current scope, which almost certainly doesn't exist here, leading to a NameError when the admin loads.
Fix this quickly by wrapping them in quotes:
list_display = ('username', 'email', 'first_name', 'last_name', 'is_active', 'roles', 'login')
2. Verify roles and login Are Valid for Your User Model
Even with quotes, you need to make sure these entries are valid for your User model:
- If
rolesis a field: Check that your User model (either Django's defaultUseror a custom one) actually has arolesfield defined. If you added it recently, don't forget to runpython manage.py makemigrationsandpython manage.py migrateto apply the changes to your database. - If
loginis a custom method: This might be a method you added to display, say, the last login time. Ensure the method exists on your User model (or the UserAdmin class) and is properly defined. For example:# In your User model def login(self): return self.last_login.strftime('%Y-%m-%d %H:%M') if self.last_login else 'Never logged in'
3. Check UserAdmin Field Configuration
When overriding UserAdmin, it's easy to accidentally omit required fields from the admin's editable sections. If roles is a field you want to edit when saving users, make sure it's included in the fieldsets (for existing users) and add_fieldsets (for new users) of your custom UserAdmin:
class UserAdmin(UserAdmin): list_display = ('username', 'email', 'first_name', 'last_name', 'is_active', 'roles', 'login') # Update fieldsets to include 'roles' fieldsets = ( (None, {'fields': ('username', 'password')}), ('Personal Info', {'fields': ('first_name', 'last_name', 'email', 'roles')}), ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}), ('Important Dates', {'fields': ('last_login', 'date_joined')}), ) # If adding new users needs 'roles', update add_fieldsets too add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('username', 'email', 'password1', 'password2', 'roles', 'is_active'), }), )
4. Custom User Model Check (If Applicable)
If you're using a custom User model instead of Django's default, double-check that:
- You've set
AUTH_USER_MODEL = 'your_app.YourUserModel'in yoursettings.py - You've registered the custom model with your
UserAdmininadmin.py:from django.contrib import admin from .models import YourUserModel admin.site.register(YourUserModel, UserAdmin)
If you can share the exact error message (including the traceback) you're seeing when trying to save a user, that would help narrow down the issue even further!
内容的提问来源于stack exchange,提问作者A.J.

