Django Admin页面中自定义User模型与Patient模型显示名称重复问题求助
解决Django Admin中Patient模型显示为"Users"的问题
我来帮你搞定这个问题~你遇到的核心原因是Django多表继承默认会让子类继承父类的显示名称配置,所以Patient模型自动沿用了User的verbose_name_plural(复数显示名),导致Admin里两个模型都显示成"Users"。
第一步:给Patient模型添加自定义显示名称
修改你的Patient模型,添加Meta类明确指定它的单复数显示名:
class Patient(User): age = models.IntegerField(null=True) class Meta: verbose_name = "Patient" # 单数显示名 verbose_name_plural = "Patients" # 复数显示名,这是Admin侧边栏显示的名称
第二步:调整CustomPatientAdmin类(可选但推荐)
你的CustomPatientAdmin继承自UserAdmin是可行的,但可以稍微调整下,让它更贴合Patient模型的特性:
class CustomPatientAdmin(UserAdmin): model = Patient add_form = CustomPatientCreationForm # 确保Admin读取模型的自定义显示名称(模型设置后这里可省略,加上更保险) verbose_name = "Patient" verbose_name_plural = "Patients" fieldsets = ( *UserAdmin.fieldsets, ( '患者额外信息', # 把标题改成更贴合场景的名称 { 'fields': ( 'is_patient', 'is_regular', 'phoneNumber', 'city', 'age', ) } ) )
第三步:验证表单配置(确保创建逻辑正确)
检查你的CustomPatientCreationForm,要确保它关联的是Patient模型而非父类User:
class CustomPatientCreationForm(UserCreationForm): class Meta: model = Patient # 包含Patient需要的所有字段,包括继承自User的和自定义的age fields = ('username', 'email', 'phoneNumber', 'city', 'age')
做完这些修改后,重启Django服务器,刷新Admin页面,你就能看到侧边栏里分别显示"Users"和"Patients"两个独立的管理入口啦~
内容的提问来源于stack exchange,提问作者Sam
相关产品推荐
相关产品推荐

