from django.contrib.auth.models import User from django.db import models # Create your models here. from mptt.models import MPTTModel #部门表 class Dapt(MPTTModel): name = models.CharField(max_length=50) parent = models.ForeignKey('self', verbose_name='上级部门', null=True, blank=True, related_name='pid', on_delete=models.CASCADE) def setObject(self, data): for key, val in data.items(): if hasattr(self, key): self.__setattr__(key, val) return self #岗位 class Post(models.Model): name = models.CharField(max_length=50) #部门岗位关联表 class DaptPost(models.Model): dapt = models.ForeignKey('Dapt', verbose_name='部门', null=False, blank=False, related_name='dp_dapt', on_delete=models.CASCADE) post = models.ForeignKey('Post', verbose_name='岗位', null=False, blank=False, related_name='dp_post', on_delete=models.CASCADE) class Meta: unique_together = ('dapt', 'post') #部门人员关联表 class DaptUser(models.Model): dapt = models.ForeignKey('Dapt', verbose_name='部门', null=False, blank=False, related_name='du_dapt', on_delete=models.CASCADE) user = models.ForeignKey(User, verbose_name='人员', null=False, blank=False, related_name='du_user', on_delete=models.CASCADE) def setObject(self, data): for key, val in data.items(): if hasattr(self, key): self.__setattr__(key, val) return self class Meta: unique_together = ('dapt', 'user') #岗位人员关联表 class PostUser(models.Model): post = models.ForeignKey('Post', verbose_name='岗位', null=False, blank=False, related_name='pu_post', on_delete=models.CASCADE) user = models.ForeignKey(User, verbose_name='人员', null=False, blank=False, related_name='pu_user', on_delete=models.CASCADE) class Meta: unique_together = ('post', 'user')