Переглянути джерело

7-22作业第一步:建表(主表和表关联)

DYaiu 4 роки тому
батько
коміт
30b32acd5d

+ 2 - 1
my_project/settings.py

@@ -39,7 +39,8 @@ INSTALLED_APPS = [
     'django_filters',
     'workreport',
     'mptt',
-    'workTtree'
+    'workTtree',
+    'treeAndTable'
 ]
 
 MIDDLEWARE = [

+ 73 - 0
treeAndTable/migrations/0001_initial.py

@@ -0,0 +1,73 @@
+# Generated by Django 3.0.8 on 2020-07-23 08:39
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+import mptt.fields
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Department',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('name', models.CharField(max_length=20)),
+                ('lft', models.PositiveIntegerField(editable=False)),
+                ('rght', models.PositiveIntegerField(editable=False)),
+                ('tree_id', models.PositiveIntegerField(db_index=True, editable=False)),
+                ('level', models.PositiveIntegerField(editable=False)),
+                ('parent', mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='treeAndTable.Department')),
+            ],
+            options={
+                'verbose_name': '部门表',
+                'verbose_name_plural': '部门列表',
+            },
+        ),
+        migrations.CreateModel(
+            name='Job',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('name', models.CharField(max_length=20)),
+            ],
+            options={
+                'verbose_name': '岗位表',
+                'verbose_name_plural': '学校部门树形结构列表',
+            },
+        ),
+        migrations.CreateModel(
+            name='Dept_job',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('dept', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='treeAndTable.Department')),
+                ('job', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='treeAndTable.Job')),
+            ],
+            options={
+                'verbose_name': '部门岗位表',
+                'verbose_name_plural': '部门岗位关联表',
+                'db_table': 'dept_job',
+                'unique_together': {('dept', 'job')},
+            },
+        ),
+        migrations.CreateModel(
+            name='Dept_job_user',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('dept_job', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='treeAndTable.Dept_job')),
+                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
+            ],
+            options={
+                'verbose_name': '部门岗位关联人员表',
+                'verbose_name_plural': '部门岗位关联人员关联表',
+                'db_table': 'dept_job_user',
+                'unique_together': {('dept_job', 'user')},
+            },
+        ),
+    ]

+ 54 - 1
treeAndTable/models.py

@@ -1,3 +1,56 @@
+from django.contrib.auth.models import User
+from mptt.fields import TreeForeignKey
+from mptt.models import MPTTModel
 from django.db import models
 
-# Create your models here.
+
+# 岗位表
+class Job(models.Model):
+    name = models.CharField(max_length=20)
+
+    class Meta:
+        # db_table = 'job'
+        verbose_name = '岗位表'
+        verbose_name_plural = '学校部门树形结构列表'
+
+
+# 部门表
+class Department(MPTTModel):
+    name = models.CharField(max_length=20)
+    # 本表的树状
+    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)
+
+    class Meta:
+        # db_table = 'department'
+        verbose_name = '部门表'
+        verbose_name_plural = '部门列表'
+
+
+# 部门岗位中间表
+class Dept_job(models.Model):
+    # 部门外键
+    dept = models.ForeignKey('Department', on_delete=models.CASCADE)
+
+    # 岗位外键
+    job = models.ForeignKey('Job', on_delete=models.CASCADE)
+
+    class Meta:
+        db_table = 'dept_job'
+        verbose_name = '部门岗位表'
+        verbose_name_plural = '部门岗位关联表'
+        unique_together = ('dept', 'job')
+
+
+# 部门岗位中间表加人员的中间表
+class Dept_job_user(models.Model):
+    # 部门外键
+    dept_job = models.ForeignKey('Dept_job', on_delete=models.CASCADE)
+
+    # 人员外键
+    user = models.ForeignKey(User, on_delete=models.CASCADE)
+
+    class Meta:
+        db_table = 'dept_job_user'
+        verbose_name = '部门岗位关联人员表'
+        verbose_name_plural = '部门岗位关联人员关联表'
+        unique_together = ('dept_job', 'user')