英文:
additional field not showing in django admin panel
问题
class CustomUser(AbstractUser):
is_admin = models.BooleanField(default=False)
company = models.ForeignKey(Company, on_delete=models.CASCADE, null=True)
department = models.ForeignKey(Department, on_delete=models.CASCADE, null=True)
contact = models.CharField(max_length=17, null=True)
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = '__all__'
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = '__all__'
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['username', 'email', 'is_staff', 'is_admin', 'department_id', 'company_id', 'contact']
list_filter = ['is_staff', 'is_admin']
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('is_admin', 'department', 'company', 'contact')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2', 'is_admin', 'department', 'company'),
}),
)
你可以尝试使用上面的代码来添加联系字段。希望这可以解决你的问题。
英文:
class CustomUser(AbstractUser):
is_admin = models.BooleanField(default=False)
company = models.ForeignKey(Company,on_delete=models.CASCADE,null=True)
department = models.ForeignKey(Department,on_delete=models.CASCADE,null=True)
contact = models.CharField(max_length=17,null=True)
whenever i tried to add a user through Djangoo admin site the contact field is not showing
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = '__all__'
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = '__all__'
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['username', 'email', 'is_staff', 'is_admin','department_id','company_id',]
list_filter = ['is_staff', 'is_admin']
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('is_admin','department_id','company_id','contact',)}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2', 'is_admin','department_id','company_id',),
}),
)
this is what i used to add field , i tried most of the thing to add this fields to Django user admin but not showing is there any mistake in my code
答案1
得分: 1
需要将该字段添加到add_fieldset
中,以及在想要启用更改它时添加到fieldsets
中:
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = [
'username',
'email',
'is_staff',
'is_admin',
'department_id',
'company_id',
]
list_filter = ['is_staff', 'is_admin']
fieldsets = UserAdmin.fieldsets[1:] + (
(
None,
{
'fields': (
'username',
'password',
'is_admin',
'department_id',
'company_id',
'contact',
)
},
),
)
add_fieldsets = (
(
None,
{
'classes': ('wide',),
'fields': (
'username',
'email',
'password1',
'password2',
'is_admin',
'department_id',
'company_id',
'contact',
),
},
),
)
话虽如此,不清楚为什么要添加一个is_admin
字段,因为Django已经有一个用于此目的的字段:is_superuser
。
英文:
You need to add the field to the add_fieldset
, as well as the fieldsets
when you want to enable to change it, with:
<pre><code>class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = [
'username',
'email',
'is_staff',
'is_admin',
'department_id',
'company_id',
]
list_filter = ['is_staff', 'is_admin']
fieldsets = UserAdmin.fieldsets<b>[1:]</b> + (
(
None,
{
'fields': (
'username',
'password',
'is_admin',
'department_id',
'company_id',
<b>'contact'</b>,
)
},
),
)
add_fieldsets = (
(
None,
{
'classes': ('wide',),
'fields': (
'username',
'email',
'password1',
'password2',
'is_admin',
'department_id',
'company_id',
<b>'contact'</b>,
),
},
),
)</code></pre>
That being said, it is unclear why you added an is_admin
, since Django already has a field for this: is_superuser
<sup>[Django-doc]</sup>.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论