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

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

additional field not showing in django admin panel

问题

  1. class CustomUser(AbstractUser):
  2. is_admin = models.BooleanField(default=False)
  3. company = models.ForeignKey(Company, on_delete=models.CASCADE, null=True)
  4. department = models.ForeignKey(Department, on_delete=models.CASCADE, null=True)
  5. contact = models.CharField(max_length=17, null=True)
  6. class CustomUserCreationForm(UserCreationForm):
  7. class Meta:
  8. model = CustomUser
  9. fields = '__all__'
  10. class CustomUserChangeForm(UserChangeForm):
  11. class Meta:
  12. model = CustomUser
  13. fields = '__all__'
  14. class CustomUserAdmin(UserAdmin):
  15. add_form = CustomUserCreationForm
  16. form = CustomUserChangeForm
  17. model = CustomUser
  18. list_display = ['username', 'email', 'is_staff', 'is_admin', 'department_id', 'company_id', 'contact']
  19. list_filter = ['is_staff', 'is_admin']
  20. fieldsets = UserAdmin.fieldsets + (
  21. (None, {'fields': ('is_admin', 'department', 'company', 'contact')}),
  22. )
  23. add_fieldsets = (
  24. (None, {
  25. 'classes': ('wide',),
  26. 'fields': ('username', 'email', 'password1', 'password2', 'is_admin', 'department', 'company'),
  27. }),
  28. )

你可以尝试使用上面的代码来添加联系字段。希望这可以解决你的问题。

英文:
  1. class CustomUser(AbstractUser):
  2. is_admin = models.BooleanField(default=False)
  3. company = models.ForeignKey(Company,on_delete=models.CASCADE,null=True)
  4. department = models.ForeignKey(Department,on_delete=models.CASCADE,null=True)
  5. 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

  1. class CustomUserCreationForm(UserCreationForm):
  2. class Meta:
  3. model = CustomUser
  4. fields = '__all__'
  5. class CustomUserChangeForm(UserChangeForm):
  6. class Meta:
  7. model = CustomUser
  8. fields = '__all__'
  9. class CustomUserAdmin(UserAdmin):
  10. add_form = CustomUserCreationForm
  11. form = CustomUserChangeForm
  12. model = CustomUser
  13. list_display = ['username', 'email', 'is_staff', 'is_admin','department_id','company_id',]
  14. list_filter = ['is_staff', 'is_admin']
  15. fieldsets = UserAdmin.fieldsets + (
  16. (None, {'fields': ('is_admin','department_id','company_id','contact',)}),
  17. )
  18. add_fieldsets = (
  19. (None, {
  20. 'classes': ('wide',),
  21. 'fields': ('username', 'email', 'password1', 'password2', 'is_admin','department_id','company_id',),
  22. }),
  23. )

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中:

  1. class CustomUserAdmin(UserAdmin):
  2. add_form = CustomUserCreationForm
  3. form = CustomUserChangeForm
  4. model = CustomUser
  5. list_display = [
  6. 'username',
  7. 'email',
  8. 'is_staff',
  9. 'is_admin',
  10. 'department_id',
  11. 'company_id',
  12. ]
  13. list_filter = ['is_staff', 'is_admin']
  14. fieldsets = UserAdmin.fieldsets[1:] + (
  15. (
  16. None,
  17. {
  18. 'fields': (
  19. 'username',
  20. 'password',
  21. 'is_admin',
  22. 'department_id',
  23. 'company_id',
  24. 'contact',
  25. )
  26. },
  27. ),
  28. )
  29. add_fieldsets = (
  30. (
  31. None,
  32. {
  33. 'classes': ('wide',),
  34. 'fields': (
  35. 'username',
  36. 'email',
  37. 'password1',
  38. 'password2',
  39. 'is_admin',
  40. 'department_id',
  41. 'company_id',
  42. 'contact',
  43. ),
  44. },
  45. ),
  46. )

话虽如此,不清楚为什么要添加一个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:

确定