How to, for example, generate:
Autor 1
+-- Livro 1
+-- Livro 2
Autor 2
+-- Livro 3
+-- Livro 4
using django-rest-framework? This in one query only?
I searched for it with query and details .. but not exactly like this ..: /
How to, for example, generate:
Autor 1
+-- Livro 1
+-- Livro 2
Autor 2
+-- Livro 3
+-- Livro 4
using django-rest-framework? This in one query only?
I searched for it with query and details .. but not exactly like this ..: /
First of all, it is good to take into consideration that your models are well-connected. To do this, ask yourself: Can I get this return using just the Django ORM? (as the friend @Adir replied).
If yes, when you are defining the serialization of your models, you should define the serialization of the relationships between them. In the tutorial on the django-rest-framework website there is a part that teaches how this relationship can be done: link
There is also this chapter in the documentation, which covers a bit more the use of relationships: link
Namely, the relationship between resources is called link (or hyperlink, or hypermedia) according to the REST maturity model, proposed by Leonard Richardson
Hello, well I think you should be talking about doing a SQL query. It is usually tricky to bring this organized data from a query but you can do this using Django's ORM if you have the right relationships.
You should probably recover the authors with something like this
autores = Autores.objects.all()
As I said if you did the right relationships you could retrieve all the books associated with the authors, something like
for autor in autores:
autor.livros.all()