rsinger86 / drf-flex-fields

Dynamically set fields and expand nested resources in Django REST Framework serializers.
MIT License
740 stars 61 forks source link

Doesn't work out of the gate #63

Closed lesreaper closed 4 years ago

lesreaper commented 4 years ago

Not sure what I'm doing wrong, but I'd really love to get this fixed after following the README pretty exact I think.

serializers.py

from rest_flex_fields import FlexFieldsModelSerializer

class CastleSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = Castle
        fields = '__all__'

class RoomSerializer(FlexFieldsModelSerializer):
    castle = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = Room
        fields = ('id', 'name', 'castle')

        expandable_fields = {
            "castle": CastleSerializer
        }

settings.py

# To make our nested serializers more like JSON API
REST_FLEX_FIELDS = {"EXPAND_PARAM": "include"}

API call: GET /api/v1/room/1/?include=castle or API call: GET /api/v1/rooms/?include=castle

both give:

{
    "id": 1,
    "name": "Throne Room",
    "castle": 1
}

OR

[
  {
    "id": 1,
    "name": "Throne Room",
    "castle": 1
  }
]

I expect it to be:

{
    "id": 1,
    "name": "Throne Room",
    "castle": {
        "id": 1,
        "name": "Daventry Castle"
    }
}

I also expect to be able to POST an update method without having to add the nested castle id and have it just validate and work. That looks like it's fixed in the latest. API call: POST /api/v1/rooms-update/

{
    "id": 1,
    "name": "Throne Room Changed Text",
}

TO RETURN

{
    "id": 1,
    "name": "Throne Room Changed Text",
    "castle": {
        "id": 1,
        "name": "Daventry Castle"
    }
}

It seems none of this is currently working. What am I missing out there (probably obvious to everyone else) to get this library working?

lesreaper commented 4 years ago

I switched to django-restql to get the functionality I needed. Sorry for the long entry!