【问题标题】:Django admin panel display error linked to admin.py?Django管理面板显示错误链接到admin.py?
【发布时间】:2018-06-03 13:14:33
【问题描述】:

我的管理面板中有一个看似无害但非常琐碎的错误:

它错误地产生输出“Teacherss”(双 ss),我无法从我的代码中看出为什么会出现这种情况:

所以教师应用中的models.py是:

class Teachers(models.Model):
    #this is what an album is going to be made of
    email=models.CharField(max_length=250)
    school_name=models.CharField(max_length=500)
    password=models.CharField(max_length=100)
    class_name=models.CharField(max_length=100)

admin.py 文件有这个:

from django.contrib import admin
from main.models import Teachers 
# Register your models here.
admin.site.register(Teachers)

知道为什么这会在管理面板中生成吗?

主要 教师添加/更改

双 ss 是从哪里来的,我该如何摆脱它!??

根据答案更新

更新:有趣的是,从下面的答案中注意到必须使用单数。但是我确实更改了我的代码,现在出现以下错误:

错误

在 admin.py 从 main.models 导入教师 导入错误:无法导入名称“教师”

admin.py 文件

from django.contrib import admin
from main.models import Teacher
# Register your models here.
admin.site.register(Teacher)

models.py

from django.db import models

# Create your models here.
class Teacher(models.Model):
    #this is what an album is going to be made of
    email=models.CharField(max_length=250)
    school_name=models.CharField(max_length=500)
    password=models.CharField(max_length=100)
    class_name=models.CharField(max_length=100)

                #You need this for meta data purposes. This allows you to reference the post (otherwise it will just print the object which doesn't mean much)
                #You need this for meta data purposes. This allows you to reference the post (otherwise it will just print the object which doesn't mean much)

...问题已解决。我没有在导入模型中调用该应用程序(编写的是 main.models 而不是 Teachers.models)。

感谢您的以下回答

【问题讨论】:

    标签: django admin models


    【解决方案1】:

    默认情况下,Django 期望您的模型具有单数名称,即Teacher。默认情况下,它还会将s 附加到您的模型名称以在管理员中显示它。这可以从inside your model itself 进行配置。

    【讨论】:

      【解决方案2】:

      Django 管理员会自动添加一个“s”来使模型复数。将您的模型设为Teacher 可能更有意义。否则,您可以告诉管理员您希望复数使用什么:

      class Teachers(models.Model):
          class Meta:
             verbose_name_plural = "teachers"
          email=models.CharField(max_length=250)
          ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-05
        • 2014-08-11
        • 2019-04-20
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        相关资源
        最近更新 更多