Skip to content
Advertisement

How to serialize a model with many to maany relashionship?

I have this function in my model,

JavaScript

but likes is actualy a many to many relashionship with User. How can I serialize it to get something like this?

JavaScript

So that I can also get rid of the ‘likes number’ property.

Advertisement

Answer

You can use list comprehension to serialize your data, but using django rest framework’s serializer is more proper.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement