Theory of the Injection of Relationships

We will learn how we can display user attributes in the output for product request.

Imagine a scenario where you go to the API to get the products, but you have to display some of the user’s information in this case.

One possible solution would be to add the attribute user_id to the product_serializer in order to get the corresponding user later. This may sound like a good idea, but if we are concerned about performance or our database transactions are not fast enough, we should reconsider this approach. It is important to understand that for each product we retrieve, we will have to retrieve its corresponding user.

Integrate into a meta attribute

The first solution is to integrate identifiers of linked users to products in a meta attribute so that we obtain a JSON like below:

Get hands-on with 1200+ tech skills courses.