附加字段未在Django管理面板中显示。

huangapple go评论74阅读模式
英文:

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&nbsp;<sup>[Django-doc]</sup>.

huangapple
  • 本文由 发表于 2023年7月23日 22:21:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定