class Department (models.Model):
name = models.CharField (maxlength = 255, null = False, blank = False)
code = models.CharField (maxlength = 15, unique = True, null = False, blank = False)
path = models.CharField (maxlength = 255, null = False, editable = False)
parent = models.ForeignKey ('self', null = True, blank = True)
def save (self):
if self.parent:
self.path = '% s% s /'% (self.parent.path, self.code)
else:
self.path = '/% s /'% self.code
super (type (self), self) .save ()
for a in Department.objects.filter (parent = self.id):
a.save ()
staff = Staff.objects.filter (department__path__startswith = department.path)
def __str __ (self):
return "% s% s"% ('------' [: self.path.count ('/', 2) -1], self.name)
Source: https://habr.com/ru/post/5959/
All Articles