estebistec / drf-compound-fields

Django-REST-framework serializer fields for compound types.
BSD 3-Clause "New" or "Revised" License
92 stars 12 forks source link

Would be great to be able to nest compound fields #21

Open erikcw opened 10 years ago

erikcw commented 10 years ago

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?

JuanjoRoig commented 10 years ago

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

erikcw commented 10 years ago

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.

estebistec commented 10 years ago

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 commented 9 years ago

@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 commented 9 years ago

Please see #27