models.py
class Employees(BaseModel):
email = models.EmailField(
null=True,
blank=True,
verbose_name='员工邮箱'
)
name = models.CharField(
max_length=128,
verbose_name="员工姓名",
null=True,
blank=True
)
mobile = models.CharField(
max_length=11,
verbose_name="手机号",
default="",
)
avatar = models.URLField(
verbose_name="头像",
blank=True,
null=True,
max_length=512,
default=""
)
department = models.ForeignKey(
to="Department",
on_delete=models.SET_NULL,
related_name="employees_in_department",
verbose_name="关联部门表",
null=True,
blank=True
)
position = models.ForeignKey(
to="EmployeesPosition",
on_delete=models.SET_NULL,
related_name="employees_in_position",
verbose_name="关联职位表",
null=True,
blank=True
)
register_time = models.DateTimeField(
auto_now_add=True,
verbose_name="注册时间"
)
add_people = models.ForeignKey(
User,
verbose_name="添加者",
related_name="linkadd_peoples",
null=True,
blank=True,
on_delete=models.SET_NULL
)
connect_user = models.OneToOneField(to='User',
verbose_name="员工关联用户表",
null=True,
blank=True,
related_name="connect_employee",
on_delete=models.SET_NULL
)
# EMPLOYEES_STATUS_OPEN = 0
# EMPLOYEES_STATUS_CLOSE = 1
# EMPLOYEES_STATUS_CHOICES = (
# (EMPLOYEES_STATUS_OPEN, 'open'),
# (EMPLOYEES_STATUS_CLOSE, 'close'),
# )
# status = models.PositiveSmallIntegerField(
# default=U,
# choices=User.USER_STATUS_CHOICES,
# help_text="{ 0:'open', 1:'close'}"
# )
class Meta:
verbose_name = "员工表"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
def get_status(self):
if self.connect_user:
return self.connect_user.status
return User.USER_STATUS_INACTIVE
def get_manage_departments(self):
return self.manage_departments.filter(tags=0)
def is_in_department(self, department_id):
if self.department and self.department.id == department_id:
return True
return False
def get_department_id(self):
return getattr(self.department, 'id', -1)
def is_department_leader(self):
if self.department and self.department.is_header(self.id):
return True
return False
def set_connect_user_inactive(self):
if self.connect_user:
return self.connect_user.set_inactive()
return None
@property
def department_name(self):
return self.department.departmentname
@property
def position_name(self):
return self.position.positionname
@property
def add_people_name(self):
return self.add_people.name
def get_department_name(self):
try:
self.department.departmentname
except Exception:
return 'null'
return self.department.departmentname
def get_position_name(self):
try:
self.position.positionname
except Exception:
return 'null'
return self.position.positionname
def get_add_people_name(self):
try:
self.add_people.name
except Exception:
return 'null'
return self.add_people.name
def get_add_people_id(self):
try:
self.add_people.id
except Exception:
return 'null'
return self.add_people.id
class EmployeesPosition(BaseModel):
positionname = models.CharField(max_length=48,
verbose_name="职位名称",
unique=True)
add_people = models.ForeignKey(User,
verbose_name="员工职位添加者",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="employee_position_add_people")
fix_people = models.ForeignKey(User,
verbose_name="最后修改者",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="employee_position_fix_people")
roles = models.ManyToManyField(to="Role",
verbose_name="职位角色",
blank=True,
null=True)
company = models.ForeignKey(to="Company",
verbose_name="公司职位表",
related_name="companys",
on_delete=models.SET_NULL,
null=True,
blank=True)
class Meta:
verbose_name = "员工职位表"
verbose_name_plural = verbose_name
def __str__(self):
return self.positionname
def employees_in_employeesposition(self):
return self.employees_in_position.filter(tags=0)
def get_employees_in_position(self):
return self.employees_in_employeesposition.filter(id=self.employees_in_position.id).count() > 0
def get_position_id(self):
return getattr(self.employees_in_position, 'id', -1)
@property
def add_people_name(self):
return self.add_people.name
@property
def fix_people_name(self):
return self.fix_people.name
@property
def position_name(self):
return self.positionname
serializer.py
class PositionSerializer(serializers.ModelSerializer): positionname = serializers.CharField( validators=[UniqueValidator(queryset=EmployeesPosition.objects.filter(tags=0))] ) add_people_name = serializers.ReadOnlyField() fix_people_name = serializers.ReadOnlyField() position_name = serializers.ReadOnlyField() create_time = serializers.DateTimeField() fix_time = serializers.DateTimeField() class Meta: model = EmployeesPosition fields = ('id', 'positionname', 'add_people', 'fix_people', 'add_people_name', 'fix_people_name', 'position_name', "create_time", "fix_time") read_only_fields = ('id', 'add_people', 'fix_people',) extra_kwargs = { "positionname": { 'required': True, } }
data
序列化外键以外字段