DjangoRestful implemented recursively nested sequence of

****

As the blog Park is not supported markdown syntax, it is recommended this link:

Original  https://blog.csdn.net/weixin_42495873/article/details/89433542

# Django serializer implemented recursively nested

** recently doing their own projects, need to return comments relevant data tables, table design my comment is this, id is empty father is the highest level of parent comment, then followed by children, grandchildren. . . Comments related information, similar to the three linkage provincial and municipal, but this depth review sheets can be deeper **

! [My comment form] (https://img-blog.csdnimg.cn/2019042114353698.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQ5NTg3Mw==,size_16,color_FFFFFF,t_70)
## # 1 is beginning to take serializer nested
1. serialize a nested much easier, but is a comparison of the number of layers, and then later are repeated, I just started following is a repeat of the sequence of a nested
! [own model class] (https://img-blog.csdnimg.cn/20190421144424984.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQ5NTg3Mw==,size_16,color_FFFFFF,t_70 )
2. the advantage is simple, but the drawback is ** comments depth not too high, but the code is too redundant **.
3. While simple to complete, and went a perfect front-end code, the front read, read, comment show the recursive implementation of the (jquery), so I think in a perfect look, see if you can use the complete sequence of recursive manner .
### 2. recursive serializer
- find information on the Internet and found a recursive sequence of an official of the way. ! [Official recursive serializer] (https://img-blog.csdnimg.cn/20190421145133406.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQ5NTg3Mw==,size_16,color_FFFFFF,t_70)
- official Links here [here] [1], but you can see, support Django1.8,1.9.2.0 ..., and I use Djnago1.11 development, it does not take. . .
- began to find information. . Very happy to find a similar ..l! [Recursive serializer] (https://img-blog.csdnimg.cn/20190421145825240.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQ5NTg3Mw== , size_16, color_FFFFFF, t_70)
- serializers.SerializerMethodField () this method seems to be a field designated to return the information you need to return, without affecting the previous model class name, more convenient and flexible with the following function uses the previously specified field. to the same function name in the specified field name ** ** get_.
- However, he returned to the parent class, and I need to return the reverse is ** ** foreign key references, the parent comment plurality of sub-comments, so serializer added many = True, then ok, only a serializer enough
`` `python
class Childrens_Conten_Serializer(ModelSerializer):
user = User_Avatar_Url_Serializer()
subs = serializers.SerializerMethodField()

class Meta:
model = Comment
fields =('id','like','subs','content','user')

get_subs DEF (Self, obj):
IF obj.subs:
return Childrens_Conten_Serializer (obj.subs, MANY = True) .data
the else:
return None
`` `
- recursive sequence of data returned, if there still may be a plurality of levels.
! [Insert Picture description here] (https://img-blog.csdnimg.cn/20191203112335923.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQ5NTg3Mw==,size_16,color_FFFFFF,t_70)
perfect realize, do not show the front-end code, write feeling thief complex, chaotic. . ! [Front page] (https://img-blog.csdnimg.cn/20190421151239305.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQ5NTg3Mw==,size_16,color_FFFFFF,t_70)
- for the first time write blog, markdown with not very good, is a technical white, multi-bug and share their problems. it also please the god pointing pointing. . .

 

[1]:https://github.com/heywbj/django-rest-framework-recursive

Guess you like

Origin www.cnblogs.com/dncey/p/11975843.html