[TimLinux] myblog 数据表格显示
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 数据表格显示的更多相关文章
- [TimLinux] myblog 首页创建
1. 设计 2. 结构 3. 实现 templates/common/layout.html: <!DOCTYPE html> <html lang="zh"&g ...
- [TimLinux] myblog 创建第一个app
1. 项目结构 项目地址:https://github.com/timscm/myblog.git 2. 启动项目 通过pycharm启动项目,进入调试模式: "D:\Program Fil ...
- .net 数据表格显示控件
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/chenjinge7/article/details/30470609 1. GridView 控件 ...
- [TimLinux] myblog 页面Axure设计
1. 导航 2. 首页主体 3. 侧边栏 4. 页尾 5. 使用工具 Axure RP 8.0.0.3312 Pro版本.
- layui之普通数据表格显示switch选择表单组件
先看效果: 一般这写什么switch组件,下拉框组件只在表单显示,如果要在其他地方显示就要注意一下细节 默默跳槽一下这个layui,真的蛋疼,每次用它东西都要各种设置东西,无语 接下来看下代码: HT ...
- 初学ExtJs 表格显示后台数据
最近开始接触ExtJs,贴出自己的代码,一个简单的表格显示 版本 3.4.1 需要json包 代码清单1.jsp引入的ExtJs文件 <!-- 资源文件 ExtJs --> <lin ...
- JQuery Easy Ui dataGrid 数据表格
数据表格 - DataGrid 英文文档:http://www.jeasyui.com/documentation/index.php# 继承$.fn.panel.defaults,使用$.fn.da ...
- TP数据访问
重点学习了: 1,ThinkPHP查询数据 2.ThinkPHP添加数据 LianXiController.class.php <?php namespace Home\Controller; ...
- JQuery Easy Ui dataGrid 数据表格 ---制作查询下拉菜单
JQuery Easy Ui dataGrid 数据表格 数据表格 - DataGrid 继承$.fn.panel.defaults,使用$.fn.datagrid.defaults重载默认值.. 数 ...
随机推荐
- 「每日五分钟,玩转 JVM」:GC 概览
前言 GC(Garbage Collection)是我们在学习 JVM 的过程中不可避免的一道坎,接下来,我们就来系统的学习一下 GC. 做一件事情之前,我们一定要去知道我们为什么要去做,这里不仅仅指 ...
- 格式工厂转化成mp4 avc格式 暴风影音不能播放的解决方法
格式工厂转化成mp4 avc格式 暴风影音不能播放的解决方法 先转成其他mp4 确保能播放 然后再转成avc
- python 快速发送大量邮件
因为公司需求,需要发送千万封级别邮件. # coding:utf-8 import csv import smtplib from email.mime.text import MIMEText im ...
- 130道ASP.NET面试题(一)
1 .简述 private,protected,public,internal修饰符的访问权限 答: private : 私有成员, 在类的内部才可以访问. protected : 保护成员,该类内部 ...
- CentOS7 编码编译安装或卸载http2.4.25 一键脚本
待完善 CentOS 7测试 哈哈 #!/bin/bash #************************************************************** #Autho ...
- c#Func委托
public delegate TResult Func<in T, out TResult>(T arg); 参数类型 T:此委托方法的参数类型 TResult:此委托方法的返回值类型 ...
- pat 1124 Raffle for Weibo Followers(20 分)
1124 Raffle for Weibo Followers(20 分) John got a full mark on PAT. He was so happy that he decided t ...
- Python3.7.1(四) Print如何在输出中插入变量
# 如果想在打印的字符串中的任意地方加入任意的变量,可以使用python的格式化输出.## 用例代码如下:s = 'Hello'x = len(s)print("The length of ...
- Component 和 PureComponent 的区别;复制demo,肉眼可以的区别
React.PureComponent它用当前与之前 props 和 state 的浅比较覆写了 shouldComponentUpdate() 的实现.简单来说,就是PureComponent简单实 ...
- python3 pip报错 TypeError: 'module' object is not callable
使用命令:python -m pip install xx即可,需要在pip前加python -m