아래에 표시된 범주와 같은 모델에 의해 주어진 트리를 직렬화하는 간단한 방법이 있습니까?Django에서 트리를 직렬화하기
[ { 'name': 'cat1',
'children': [ { 'name': 'cat11',
'children': [ ... ]
]
}
...
]
감사
class Category(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
order_key = models.IntegerField()
class Meta:
verbose_name_plural = 'Categories'
class MPTTMeta:
order_insertion_by = ['order_key']
def __unicode__(self):
return "%s" %(self.name)
고마워요. 덤프 대신 덤프를 사용하고 children_set.all() 대신 get_children()을 사용합니다. – jul
그 강아지를 목록 이해력으로 바꾸지 않는 이유는 무엇입니까? –
가독성, 그것은 코드를 읽기가 어려운 긴 줄일까요? –