Comment_list = models. Comment.objects. Filter (news_id = new_id) ret = [] # finally get the data comment_list_dict = {} # Build intermediate dictionary for row in comment_l IST: # In the data identified by the ID as key, each row of data as value to generate a dictionary row.update ({"Children": []}) # Build a key children corresponding to an empty list comment_list _dict[row["id"] = row # takes the ID as the key, the current row as the value in the dictionary for item in Comment_list: # Iterate through the list of data that is fetched Parrent_row = comment _list_dict.get (item["parent_id"]) # Get the father's address for the current line if not parrent_row: # If the father is none, go directly to the RET Ret.appe ND (item) Else: # Otherwise, take this line append to the father's children parrent_row["Children"].append (item) # Focus on this line and use the first knowledge mentioned above Recognize dot print (ret)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service