1. 设计

2. 数据

创建数据库用户:

CREATE USER IF NOT EXISTS 'user1'@'MyBlogPwd123';
GRANT ALL ON d1.* TO 'user1'@'%';

创建数据库d1:

CREATE DATABASE IF NOT EXISTS d1 DEFAULT CHARSET = utf8;

创建数据表:

USE d1;

CREATE TABLE IF NOT EXISTS score (
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(32) NOT NULL,
KEY score_name_key (`name`),
UNIQUE (`name`)
); CREATE TABLE IF NOT EXISTS student (
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`stuid` INT(8) ZEROFILL NOT NULL DEFAULT '',
`name` VARCHAR(32) NOT NULL,
`sex` BOOLEAN NOT NULL,
KEY student_name_key (`name`),
UNIQUE (`stuid`)
); CREATE TABLE IF NOT EXISTS student_score (
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`student_id` INT(11) NOT NULL,
`score_id` INT(11) NOT NULL,
`pass_flag` BOOLEAN,
KEY student_score_student_key (`student_id`),
KEY student_score_score_key (`score_id`),
CONSTRAINT student_score_student_key FOREIGN KEY (`student_id`) REFERENCES `student` (`id`),
CONSTRAINT student_score_score_key FOREIGN KEY (`score_id`) REFERENCES `score` (`id`)
);

导入数据:

INSERT INTO score VALUES
(1, '高等数学'),
(2, '军事理论'),
(3, '大学英语'),
(4, '离散数学'),
(5, '数据库导论'); INSERT INTO student VALUES
(1, '', '赵敏', 0),
(2, '', '周芷若', 0),
(3, '', '小昭', 0),
(4, '', '殷离', 1),
(5, '', '张翠山', 1),
(6, '', '谢逊', 1),
(7, '', '灭绝师太', 0),
(8, '', '张无忌', 1),
(9, '', '殷素素', 0),
(10, '', '陈友谅', 1); INSERT INTO student_score (student_id, score_id, pass_flag) VALUES
(1, 1, 1), (1, 2, 0), (1, 3, 1), (1, 4, 1), (1, 5, 0),
(2, 1, 1), (2, 2, 1), (2, 3, 1), (2, 4, 1), (2, 5, 1),
(3, 1, 0), (3, 2, 1), (3, 3, 1), (3, 4, 1), (3, 5, 0),
(4, 1, 1), (4, 2, 0), (4, 3, 1), (4, 4, 1), (4, 5, 0),
(5, 1, 1), (5, 2, 0), (5, 3, 1), (5, 4, 1), (5, 5, 0),
(6, 1, 1), (6, 2, 0), (6, 3, 1), (6, 4, 1), (6, 5, 0),
(7, 1, 1), (7, 2, 0), (7, 3, 1), (7, 4, 1), (7, 5, 0),
(8, 1, 1), (8, 2, 0), (8, 3, 1), (8, 4, 1), (8, 5, 0),
(9, 1, 1), (9, 2, 0), (9, 3, 1), (9, 4, 1), (9, 5, 0),
(10, 1, 1), (10, 2, 0), (10, 3, 1), (10, 4, 1), (10, 5, 0);

3. 结构

ttt

4. 实现

polls/models/__init__.py:

from .student import Score, Student, StudentScore

polls/models/student.py

from django.db import models

class Score(models.Model):
name = models.CharField(max_length=32, null=False, blank=False, unique=True) def __str__(self):
return self.name class Meta:
db_table = 'score' class Student(models.Model):
stu_id = models.IntegerField(null=False, blank=False, default='')
name = models.CharField(max_length=32, null=False, blank=False, unique=True)
sex = models.BooleanField() # True: 男, False: 女
score = models.ManyToManyField(Score, through="StudentScore") def __str__(self):
return self.name class Meta:
db_table = "student" class StudentScore(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
score = models.ForeignKey(Score, on_delete=models.CASCADE)
pass_flag = models.BooleanField() # True: pass, False: fail def __str__(self):
return "%s %s" % (self.student.name, self.score.name) class Meta:
db_table = "student_score"

开始迁移数据:

D:\pycharm\myblog>python manage.py makemigrations
Migrations for 'polls':
polls\migrations\0001_initial.py
- Create model Score
- Create model Student
- Create model StudentScore
- Add field score to student D:\pycharm\myblog>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying polls.0001_initial... OK
Applying sessions.0001_initial... OK

进入MySQL导入默认数据:python manange.py dbshell,执行上面提到的INSERT INTO 语句

更新polls/views.py:

ttt

5. 效果

ttt

[TimLinux] myblog 数据表格显示的更多相关文章

  1. [TimLinux] myblog 首页创建

    1. 设计 2. 结构 3. 实现 templates/common/layout.html: <!DOCTYPE html> <html lang="zh"&g ...

  2. [TimLinux] myblog 创建第一个app

    1. 项目结构 项目地址:https://github.com/timscm/myblog.git 2. 启动项目 通过pycharm启动项目,进入调试模式: "D:\Program Fil ...

  3. .net 数据表格显示控件

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/chenjinge7/article/details/30470609 1. GridView 控件 ...

  4. [TimLinux] myblog 页面Axure设计

    1. 导航 2. 首页主体 3. 侧边栏 4. 页尾 5. 使用工具 Axure RP 8.0.0.3312 Pro版本.

  5. layui之普通数据表格显示switch选择表单组件

    先看效果: 一般这写什么switch组件,下拉框组件只在表单显示,如果要在其他地方显示就要注意一下细节 默默跳槽一下这个layui,真的蛋疼,每次用它东西都要各种设置东西,无语 接下来看下代码: HT ...

  6. 初学ExtJs 表格显示后台数据

    最近开始接触ExtJs,贴出自己的代码,一个简单的表格显示 版本 3.4.1 需要json包 代码清单1.jsp引入的ExtJs文件 <!-- 资源文件 ExtJs --> <lin ...

  7. JQuery Easy Ui dataGrid 数据表格

    数据表格 - DataGrid 英文文档:http://www.jeasyui.com/documentation/index.php# 继承$.fn.panel.defaults,使用$.fn.da ...

  8. TP数据访问

    重点学习了: 1,ThinkPHP查询数据 2.ThinkPHP添加数据 LianXiController.class.php <?php namespace Home\Controller; ...

  9. JQuery Easy Ui dataGrid 数据表格 ---制作查询下拉菜单

    JQuery Easy Ui dataGrid 数据表格 数据表格 - DataGrid 继承$.fn.panel.defaults,使用$.fn.datagrid.defaults重载默认值.. 数 ...

随机推荐

  1. mysql update获取主键

    mysql update获取主键<pre>SET @update_id := 0;UPDATE mobantestinfo1 SET info2 = 'value', id = (SELE ...

  2. maven(1)

    Maven进价:Maven的生命周期阶段 一.Maven的生命周期 Maven的生命周期就是对所有的构建过程进行抽象和统一.包含了项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等 ...

  3. 怎么把CAT客户端的RootMessageId记录到每条日志中?

    什么是RootMessageId? 为了理解RootMessageId先简单介绍一下CAT的数据结构设计.CAT客户端会将所有消息都封装为一个完整的消息树(MessageTree),消息树可能包括Tr ...

  4. PHP 7.4 新语法:箭头函数

    短闭包,也叫做箭头函数,是一种用 php 编写的短函数.当向函数中传递闭包时,这个功能是非常有用的,比如使用 array_map 或是 array_filter 函数时. 译者注:PHP7.4 计划于 ...

  5. nyoj 75-日期计算 (闰年与平年的判断)

    75-日期计算 内存限制:64MB 时间限制:3000ms 特判: No 通过数:19 提交数:31 难度:1 题目描述: 如题,输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第 ...

  6. nyoj 484-The Famous Clock

    484-The Famous Clock 内存限制:64MB 时间限制:1000ms 特判: No 通过数:2 提交数:2 难度:1 题目描述: Mr. B, Mr. G and Mr. M are ...

  7. RGW Data Layout

    目录 Overview metadata bucket index data RGW Pools RGW Object References Overview RGW 中三个基本概念:user, bu ...

  8. C语言入门教程: 一个简单的实例

    对于学习要保持敬畏! 语言不只是一种工具,还是一种资源,因此,善待它,掌握它!   我们知道,对于未知通常都会充满好奇和畏惧,既想了解它,又害怕神秘面纱隐藏的不确定性.对于一门编程语言同样如此,我将以 ...

  9. Vue注册组件命名时不能用大写的原因浅析

    命名使用注意事项: https://www.jb51.net/article/160227.htm

  10. Linux菜鸟——常见命令一 权限

    Linux对文件和目录的权限位 权限位是十位 第一位 代表文件类型 - 普通文件 d 目录文件 l 链接文件 后面九尾 所有者权限 u = user 所属组权限 g = group 其他人权限 o = ...