Django serializer depth doesn't serialize nested models
I use django and react and I want to make some workspaces where they have inside channel instances.
Models.py
class Workspace(models.Model):
channels = models.ManyToManyField('Channel', related_name='channels', blank=True)
Serializers.py
class WorkspaceSerializer(serializers.ModelSerializer):
class Meta:
model = Workspace
fields = '__all__'
depth = 2
Views.py
@api_view(['POST'])
def getworkspace(request):
if request.method == 'POST':
workspace = Workspace.objects.filter(id=request.POST['id']).first()
serializer = WorkspaceSerializer(workspace, many=False)
return Response(serializer.data)
Then I use axios to fetch the data
axios.post('http://127.0.0.1:8000/getworkspace/', formData, {headers: {'Content-type': 'multipart/form-data'}}).then(res=>{
setWorkspace(res?.data)
})
console.log(workspace)
What I was expecting was for workspace channel to be serialized and me be able to access it but instead I get this {id, 1, channels: [1]}
What can I do, I have changed the depth to even 10 but it still doesn't work. If I use shell and serialize it from there like this:
from main.models import *
from main.serializers import *
workspace = Workspace.objects.filter(id=1).first()
serializer = WorkspaceSerializer(workspace, many=False)
print(serializer.data)
It works perfectly.
I believe the problem starts from the view because when I print print(serializer.data)
from there channels isn't being serialized.