You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法从Admin保存Django代理用户模型,请求协助排查错误

Troubleshooting Django User Save Error in Admin

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 roles is a field: Check that your User model (either Django's default User or a custom one) actually has a roles field defined. If you added it recently, don't forget to run python manage.py makemigrations and python manage.py migrate to apply the changes to your database.
  • If login is 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 your settings.py
  • You've registered the custom model with your UserAdmin in admin.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.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:31:01