TransWikia.com

Is it a good idea to nest custom serializer classes in django?

Stack Overflow Asked by John Cymmer on December 23, 2021

So I have this code and it is working:

class X(serializers.Serializer):
    class Y(serializers.Serializer):
        class Z(serializers.Serializer):
            id = serializers.IntegerField()
            description = serializers.CharField()

        stdout = serializers.CharField(allow_null=True, allow_blank=True)
        time = serializers.FloatField()
        memory = serializers.FloatField()
        stderr = serializers.CharField(allow_null=True, allow_blank=True)
        token = serializers.CharField()
        compile_output = serializers.CharField(allow_null=True, allow_blank=True)
        message = serializers.CharField(allow_null=True, allow_blank=True)
        status = Z()

    submissions = Y(many=True)

But is this a good idea to nest serializer classes? The nested classes(Y and Z) are only used within X class.

One Answer

I believe it is workable, but there is a better format. Instead of throwing everything in one class, it is better to have them declared as separate classes and then include it in your serializer.

Referenced from docs.

Answered by crimsonpython24 on December 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP