Django 简明教程
Django Admin – Include Models
当使用 startproject command 初始化新项目时,Django 会自动安装一些应用程序,这些应用程序的列表可以在项目的 settings module 的 INSTALLED_APPS 参数中找到。
When a new project is initialized with the startproject command, Django automatically installs a few apps, the list of these apps can be found in the INSTALLED_APPS parameter of the project’s settings module.
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
为了能够登录到管理站点,模型 – 组和用户会自动在管理站点上注册。
To be able to log into the admin site, the models – Groups and Users are automatically registered with the Admin site.
因此,当我们使用 superuser 凭证通过 URL http://localhost:8000/admin 登录到管理站点时,我们在主页上看到组和用户表格。
Hence, as we log into the admin site at the URL http://localhost:8000/admin with the superuser credentials, we get to see the Groups and Users tables on the homepage.
但是,在其他应用程序中声明的模型不会自动注册。您需要在应用程序 package folder 中存在的 “ admin.py ” 模块中执行此操作。
However, the models declared in the other apps are not automatically registered. You need to do so in the "admin.py" module present in the app’s package folder.
首先,我们创建一个新的 Django 应用程序:
First, we create a new Django app −
Python manage.py startapp myapp
接下来,我们将它包含在 INSTALLED_APPS 列表中。
Next, we include it in the list of INSTALLED_APPS.
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]
要使用的所有模型都定义在 “ models.py ” 文件中。让我们按如下方式定义员工模型:
All the models to be used are defined in the "models.py" file. Let us define the employee model as follows −
from django.db import models
# Create your models here.
class Employee(models.Model):
empno = models.CharField(max_length=20)
empname = models.CharField(max_length=100)
contact = models.CharField(max_length=15)
salary = models.IntegerField()
joined_date = models.DateField(null=True)
class Meta:
db_table = "employee"
我们必须创建 migration script 并运行迁移。
We must create the migration script and run the migrations.
python manage.py makemigrations myapp
python manage.py migrate
现在这将创建 Employee 模型。我们现在必须将此模型添加到管理界面。为此,请打开 “admin.py” 文件,导入 employee 模型,并调用 admin.register() 函数。
This will now create the Employee model. We now have to add this model to the admin interface. For that, open the "admin.py" file, import the employee model, and call the admin.register() function.
from django.contrib import admin
# Register your models here.
from .models import Employee
admin.site.register(Employee)
采取这些步骤之后,启动 Django 服务器 −
After taking these steps, start the Django server −
Python manage.py runserver
打开浏览器,并访问管理程序 URL http://localhost:8000/admin ,该 URL 现在将在 MYAPP 下显示新注册的模型。
Open the browser and visit the admin URL http://localhost:8000/admin which will now show the newly registered model under MYAPP.

要 add new employee objects ,请单击 + Add button −
To add new employee objects, click the + Add button −

单击 Employees 模型以扩展其集合 −
Click the Employees model to expand its collection −

以上列表显示“Employee object (1)”, “Employee object (2)”而没有任何详细信息,因为这是对象的默认字符串表示形式。
The list above shows "Employee object (1)", "Employee object (2)" without any details as it is the default string representation of the object.
为了显示更有意义的表示形式,我们可以在 employee 模型中添加 str() 方法。
In order to display more meaningful representations, we can add a str() method in the employee model.