rsinger86/drf-flex-fields

Undocumented feature: HyperlinkedRelatedField works as expected

Semprini opened this issue · 1 comments

The FlexFieldsModelSerializer returns values for related fields by default:

GET /api/location/region/AKL/

{
    "url": "http://127.0.0.1:8000/api/location/region/AKL/",
    "code": "AKL",
    "name": "Auckland",
    "country": "NZL"
}

But by specifying country as a HyperlinkedRelatedField:

class RegionSerializer(FlexFieldsModelSerializer):
    country = serializers.HyperlinkedRelatedField(
        read_only=True,
        view_name='country-detail'
    )

We get:

{
    "url": "http://127.0.0.1:8000/api/location/region/AKL/",
    "code": "AKL",
    "name": "Auckland",
    "country": "http://127.0.0.1:8000/api/location/country/NZL/"
}

and ?expand=country works as expected. Nice.

Interesting, it didn't occur to me this would be unexpected.

I can update the docs to make this more clear.

Default field definitions of fields will work as expected, but when expended, they will be overriden by the expanded version.