Skip to content

Commit 09a4ec0

Browse files
committed
添加了第32天的代码
1 parent a0e51bf commit 09a4ec0

File tree

20 files changed

+398
-0
lines changed

20 files changed

+398
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
venv
2+
.idea
3+
*.pyc

Day32/oa/hrs/__init__.py

Whitespace-only changes.

Day32/oa/hrs/admin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.contrib import admin
2+
3+
from hrs.models import Dept, Emp
4+
5+
6+
class DeptAdmin(admin.ModelAdmin):
7+
8+
list_display = ('no', 'name', 'location')
9+
ordering = ('no', )
10+
11+
12+
class EmpAdmin(admin.ModelAdmin):
13+
14+
list_display = ('no', 'name', 'job', 'sal', 'dept')
15+
search_fields = ('name', 'job')
16+
ordering = ('dept', )
17+
18+
19+
admin.site.register(Dept, DeptAdmin)
20+
admin.site.register(Emp, EmpAdmin)

Day32/oa/hrs/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class HrsConfig(AppConfig):
5+
name = 'hrs'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Generated by Django 2.0.5 on 2018-05-22 03:07
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
initial = True
10+
11+
dependencies = [
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='Dept',
17+
fields=[
18+
('no', models.IntegerField(primary_key=True, serialize=False)),
19+
('name', models.CharField(max_length=20)),
20+
('location', models.CharField(max_length=10)),
21+
],
22+
options={
23+
'db_table': 'tb_dept',
24+
},
25+
),
26+
migrations.CreateModel(
27+
name='Emp',
28+
fields=[
29+
('no', models.IntegerField(primary_key=True, serialize=False)),
30+
('name', models.CharField(max_length=20)),
31+
('job', models.CharField(max_length=10)),
32+
('mgr', models.IntegerField(null=True)),
33+
('sal', models.DecimalField(decimal_places=2, max_digits=7)),
34+
('comm', models.DecimalField(decimal_places=2, max_digits=7, null=True)),
35+
('dept', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='hrs.Dept')),
36+
],
37+
options={
38+
'db_table': 'tb_emp',
39+
},
40+
),
41+
]

Day32/oa/hrs/migrations/__init__.py

Whitespace-only changes.

Day32/oa/hrs/models.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from django.db import models
2+
3+
# ORM - 对象关系映射
4+
# 对象模型 <---> 关系模型
5+
# 实体类 <---> 二维表
6+
# 属性 <---> 列
7+
# 对象 <---> 记录
8+
9+
10+
class Dept(models.Model):
11+
no = models.IntegerField(primary_key=True, verbose_name='部门编号')
12+
name = models.CharField(max_length=20, verbose_name='部门名称')
13+
location = models.CharField(max_length=10, verbose_name='部门所在地')
14+
# excellent = models.BooleanField(default=0, verbose_name='是否优秀')
15+
16+
def __str__(self):
17+
return self.name
18+
19+
class Meta:
20+
db_table = 'tb_dept'
21+
22+
23+
class Emp(models.Model):
24+
no = models.IntegerField(primary_key=True)
25+
name = models.CharField(max_length=20)
26+
job = models.CharField(max_length=10)
27+
mgr = models.IntegerField(null=True, blank=True)
28+
sal = models.DecimalField(max_digits=7, decimal_places=2)
29+
comm = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
30+
dept = models.ForeignKey(Dept, on_delete=models.PROTECT)
31+
32+
class Meta:
33+
db_table = 'tb_emp'

Day32/oa/hrs/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

Day32/oa/hrs/views.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from django.shortcuts import render
2+
3+
from hrs.models import Dept, Emp
4+
5+
6+
def index(request):
7+
ctx = {
8+
'greeting': '你好,世界!'
9+
}
10+
return render(request, 'index.html', context=ctx)
11+
12+
13+
def emps(request):
14+
dno = int(request.GET['dno'])
15+
16+
17+
def depts(request):
18+
# DRY - Don't Repeat Yourself
19+
# ORM - Object Relation Mapping
20+
ctx = {'dept_list': Dept.objects.all()}
21+
return render(request, 'dept.html', context=ctx)

Day32/oa/manage.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oa.settings")
7+
try:
8+
from django.core.management import execute_from_command_line
9+
except ImportError as exc:
10+
raise ImportError(
11+
"Couldn't import Django. Are you sure it's installed and "
12+
"available on your PYTHONPATH environment variable? Did you "
13+
"forget to activate a virtual environment?"
14+
) from exc
15+
execute_from_command_line(sys.argv)

0 commit comments

Comments
 (0)