github 链接:https://github.com/heywbj/django-rest-framework-recursive
无限递归序列化(python默认最大递归层数998)可指定递归层数
serializer.py
from rest_framework_recursive.fields import RecursiveField class DepartmentTreeSerializer(serializers.Serializer): id = serializers.IntegerField() departmentname = serializers.CharField(max_length=256) parentdepartment = serializers.IntegerField(source='parentdepartment.id', allow_null=True) sub = serializers.ListField(source='get_sub_departments', child=RecursiveField()) class Meta: model = Department
models.py
class Department(BaseModel): departmentname = models.CharField(max_length=128, verbose_name="部门表名字", null=True, blank=True) header = models.ForeignKey(to="Employees", verbose_name="部门负责人(Employee)", null=True, blank=True, on_delete=models.SET_NULL, related_name="manage_departments") parentdepartment = models.ForeignKey(to="self", verbose_name="父部门", on_delete=models.SET_NULL, null=True, blank=True, related_name="sub") create_people = models.ForeignKey(User, verbose_name="部门创建者", on_delete=models.SET_NULL, null=True, blank=True, related_name="create_people") is_root = models.BooleanField(default=False) class Meta: verbose_name = "部门表" verbose_name_plural = verbose_name def __str__(self): return self.departmentname def get_sub_departments(self): return self.sub.filter(tags=0) def get_employees_in_department(self): return self.employees_in_department.filter(tags=0) def has_employee(self, employee_id): return self.get_employees_in_department().filter(id=employee_id).count() > 0 def is_header(self, employee_id): if employee_id > 0 and getattr(self.header, 'id', -1) == employee_id: return True return False
data
{ "code": 0, "error_msg": "", "data": { "count": 1, "next": null, "previous": null, "results": [ { "id": 6, "departmentname": "f", "parentdepartment": null, "sub": [ { "id": 1, "departmentname": "a", "parentdepartment": 6, "sub": [] }, { "id": 3, "departmentname": "洗澡部", "parentdepartment": 6, "sub": [] }, { "id": 4, "departmentname": "d", "parentdepartment": 6, "sub": [] }, { "id": 14, "departmentname": "砸蛋部", "parentdepartment": 6, "sub": [] }, { "id": 15, "departmentname": "8", "parentdepartment": 6, "sub": [] }, { "id": 11, "departmentname": "dddd", "parentdepartment": 6, "sub": [] }, { "id": 5, "departmentname": "e", "parentdepartment": 6, "sub": [] }, { "id": 10, "departmentname": "l", "parentdepartment": 6, "sub": [] }, { "id": 9, "departmentname": "n", "parentdepartment": 6, "sub": [ { "id": 18, "departmentname": "会计部", "parentdepartment": 9, "sub": [] }, { "id": 20, "departmentname": "会计部2", "parentdepartment": 9, "sub": [] } ] }, { "id": 8, "departmentname": "h", "parentdepartment": 6, "sub": [ { "id": 19, "departmentname": "会计部1", "parentdepartment": 8, "sub": [] } ] }, { "id": 7, "departmentname": "g", "parentdepartment": 6, "sub": [ { "id": 17, "departmentname": "枫叶路", "parentdepartment": 7, "sub": [] }, { "id": 21, "departmentname": "eat部", "parentdepartment": 7, "sub": [ { "id": 22, "departmentname": "fffffffff", "parentdepartment": 21, "sub": [] } ] } ] }, { "id": 16, "departmentname": "小虎队", "parentdepartment": 6, "sub": [] } ] } ] } }
example
from rest_framework import serializers from rest_framework_recursive.fields import RecursiveField class TreeSerializer(serializers.Serializer): name = serializers.CharField() children = serializers.ListField(child=RecursiveField())
Requirements
Python (Tested on 2.7, 3.4, 3.6) Django (Tested on 1.8, 1.9, 2.0) Django REST Framework (Tested on 3.3, 3.7)
Installation
pip install djangorestframework-recursive