S
S
Siberian_12018-09-16 14:02:53
Django
Siberian_1, 2018-09-16 14:02:53

How to make a subquery in Django to fetch all fields from a related model?

Let's say there is a post model in which there is a record field (this is the ManyToMany field). When I request Posts.objects.all().get(id=myid) I get the post object with the specified id. But the record field contains only the id of the records, but I need it to contain all the fields of this very record. I found the QuerySet select_related and prefeth_related methods on the Internet. As I understand it, you need to use the latter.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2018-09-16
@deliro

And she contains. It's just that the descriptor field (the same M2M connection) creates a request only if this field is accessed (or by explicitly calling prefetch_related). What is the actual task? Serialize data and send it via API? Use Django REST framework, it's made for this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question