[Fixed]-DRF nested serializer put very deep field in top level without intermediary layers

1👍

You can use a SerializerMethodField

my_h = serializers.SerializerMethodField()


def get_my_h(self, obj):
    return obj.b.c.d.e.f.g.h or some_better_logic_here_to_actually_get_h

The actual logic above you’ll need to tweak to suit your needs (and I wouldn’t expect it to perform well due to the level you’re trying to reach)… but essentially obj is the current instance you’re serializing so you can work with it as you wish

👤Sayse

Leave a comment