1、準備工做python
1.建立應用django
python manage.py startapp test
2.定義模型類app
(1)打開剛建立的app目錄test,打開models.py文件編碼
(2)代碼以下spa
from django.db import models class classes(models.Model): name = models.CharFild(max_length=32) class student(models.Model): name = models.CharField(max_length=32) sex = models.BooleanField() class_name = models.ForeignKey(classes, on_delete=models.CASCADE)
(3)激活模型:編輯settings.py文件,將test應用加入到installed_apps中code
(4)生成遷移文件:blog
python manage.py makemigrations
(5)執行遷移:it
python manage.py migrate
3.生成後臺:io
(1)建立管理員用戶:class
python manage.py createsuperuser
(2)管理界面本地化:
編輯settings.py文件,設置編碼、時區 LANGUAGE_CODE = 'zh-Hans' TIME_ZONE = 'Asia/Shanghai'
(3)向admin註冊模型
#打開test/admin.py文件,註冊模型 from django.contrib import admin from .models import * admin.site.register(BookInfo, BookInfoAdmin) admin.site.reigster(HeroInfo) #class HeroInfoInline(admin.StackedInline): # 正常表單 class HeroInfoInline(admin.TabularInline): # 表格添加多個關聯 model = HeroInfo extra = 3 class BookInfoAdmin(admin.ModelAdmin): list_display = ['id', 'btitle', 'bpub_date'] # 列表顯示數據 list_filter = ['btitle'] # 過濾字段 search_fields = ['btitle'] # 搜索字段 list_per_page = 1 # 每頁條數 #fields = ['bpub_date', 'btitle'] # 添加修改頁字段順序 fieldsets = [ # 添加列表分組 ('base', {'fields':['btitle']}), ('super', {'fields':['bpub_date']}) ] inlines = [HeroInfoInline] # 關聯添加