How can I get the custom nested data in Django?


I have four model as bellow:

class AModel(models.Model): name = models.CharField(max_length=11) class BModel(models.Model): name = models.CharField(max_length=11) a = models.ForeignKey(AModel, related_name="bs") class CModel(models.Model): name = models.CharField(max_length=11) b = models.ForeignKey(BModel, related_name="cs") class DModel(model.Model): name = models.CharField(max_length=11) c = models.ForeignKey(CModel, related_name="ds")

Now I want to get the bellow data:

[ {"name":"a1", "value":1, "image":"xxxxx.png", "children":[ {"name":"b1", "value":1, "image":"xxxxx.png", "children":[ {"name":"c1", "value":1, "image":"xxxx.png", "children":[ {"name":"d1", "value":1, "image":"xxxx.png", } ] } ] } ] } ]

note, the value and image key-value are add by myself. I know use the Django-Rest-Framework can get the data like:

[ { "id":1, "name":"a1", "bs":[ { "id":1, "name":"b1", "cs":[ "id":1, "name":"c1", "ds":[ { "id":1, "name":"d1", } ] ] } ] } ]

But how can I get my requirement data?

I also tried query the AModel instance first, the forloop the AModel instance's bs, and then do the next, but I find that is too complex, not a simple and convenient way to get it.


It is not the actual code but pseudo code which will give you the idea.

data_of_C_in_D = D.C_set # gives all value of C in D Data_of_B_in_C = for i in data_of_C_in_D: B.i_set #gives all value of C in B


Similarly you can go from D -- > A by making the set of each of them and then iterating over them. Check here for more

<a href="https://stackoverflow.com/questions/15306897/django-reverse-lookup-of-foreign-keys" rel="nofollow">Django reverse lookup of foreign keys</a>

It is called foreign key reverse lookup


  • Distcp Mismatch in length of source
  • how to send the response to browser fromm http.request in node.js?
  • FQL returning empty set?
  • Compare Dictionary
  • Omit template arguments when create an instance of template class from another instance of template
  • Argument of '#selector' does not refer to an '@objc' method (swift 3)
  • Tips for creating scalable WPF user control
  • Django how to modify database records by template
  • How to calculate total Fridays between two dates in excel on weekly, biweekly and monthly basis?
  • PHP Handling Namespace with SimpleXML
  • JOOQ nested condition
  • Angular2 emit event up to the DOM tree
  • Firebase, only get new children
  • How to set `secure` and `httpOnly` for Plones `__ac` cookie?
  • Why is django manage.py syncdb failing to create new columns on my development server?
  • Excel distinct count with conditions
  • Inversing an interpolation of rotation
  • Django return user model id with L
  • several dataProvider per one Test in TestNG
  • joining two bezier curves
  • Floated image with variable width and heading with background image
  • How to disable all widgets inside Panel or inside Composite?
  • Implementation of State Monad
  • Using a canvas object in a thread to do simple animations - Java
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Django simple Captcha “No module named fields” error
  • How to have background script and something similar to a default popup?
  • Python urlparse: small issue
  • FileReader+canvas image loading problem
  • VBA Convert delimiter text file to Excel
  • GridView Sorting works once only
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • embed rChart in Markdown
  • Android Google Maps API OnLocationChanged only called once
  • python draw pie shapes with colour filled
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app