Consultation that feeds grid and subgrid?

Asked

Viewed 148 times

1

How, for example, to generate:

Autor 1
+-- Livro 1
+-- Livro 2

Autor 2
+-- Livro 3
+-- Livro 4

using Django-Rest-framework? This in a single query?

I searched found with consultation and details. but not exactly like this.. :/

2 answers

2

First of all it is good to take into account that your models are well connected. To do so, ask yourself: can I get this return using only Django’s ORM? (how did @Adir’s friend respond).

If yes, when you are defining the serialization of your models, you must define the serialization of the relationships between them. In the tutorial present on the Django-Rest-framework website there is a part that teaches how this relationship can be done: http://django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis#hyperlinking-our-api

There is also this chapter in the documentation, which covers a little more the use of relationships: http://django-rest-framework.org/api-guide/serializers#hyperlinkedmodelserializer

Namely: the relationship between resources (Resources, objects) is called link (or hyperlink, or Hypermedia) according to the REST maturity model, proposed by Leonard Richardson

0

Hello, well I believe you should be talking about doing an SQL query. Normally it is complicated to bring this organized data from a query but you can do this using Django’s ORM if you have made the right relationships.

You should probably recover the authors with something like this

autores = Autores.objects.all()

Like I said if you made the right relationships you can retrieve all the books associated with the authors, something like that

for autor in autores:
    autor.livros.all()
  • Thanks for the help, but the issue at hand is the use of Django-Rest-framework

Browser other questions tagged

You are not signed in. Login or sign up in order to post.