drf-compound-fields icon indicating copy to clipboard operation
drf-compound-fields copied to clipboard

Would be great to be able to nest compound fields

Open erikcw opened this issue 10 years ago • 5 comments

It would be very useful to be able to nest compound fields, such as a DictField inside of a ListField.

# [{"name": Joe, "username": joey, id: 12,}, {"name": "Sally", "username": "codehero", id: 2}]

my_field = ListField(DictField())

In my testing, this didn't seem to currently be possible. Is there a reason why it isn't supported?

erikcw avatar Sep 19 '14 01:09 erikcw

Hi, I'm looking at the same behavior too. Is it possible?

JuanjoRoig avatar Oct 02 '14 09:10 JuanjoRoig

In my case what I ended up doing was nesting serializers. Something like this.


class MyDictSerializer(serializers.Serializer):
    """
    This serializer represents my dictionary data
    """
    name = serializers.CharField()
    username = serializers.CharField()
    id = serializers.IntegerField()

class MyModel(serializers.ModelSerializer):
    my_field = MyDictSerializer(many=True)
    #...etc

The nice thing about this approach is that validators can be attached to MyDictSerializer. Seems to work pretty well.

erikcw avatar Oct 02 '14 13:10 erikcw

Just letting you know I'm not ignoring this. I plan to look at this over this weekend and see what we should do for this. At a glance at seems reasonable to me.

estebistec avatar Oct 03 '14 01:10 estebistec

@erikcw How important is it for you that this be fixed for compatibility with rest-framework 2.X, as opposed to when I upgrade this for rest-framework 3.0 (hopefully soon)?

estebistec avatar Dec 20 '14 21:12 estebistec

Please see #27

estebistec avatar Apr 05 '15 06:04 estebistec