infinityzxx
infinityzxx

Reputation: 344

Hyperlink to a Nested Router's relations

I'm trying to make a custom field to render an Hyperlink to a nested router's list view (https://github.com/alanjds/drf-nested-routers)

Usually one would use HyperlinkedRelatedField for ManyToMany relations (with many=True) but what I need/want is only one URL returned (because the endpoint is a list view), not one per related object... is that possible without re-inventing the wheel ?

I created a field (https://github.com/alanjds/drf-nested-routers/blob/master/rest_framework_nested/relations.py#L63) it does work very well for foreign keys but not for ManyToMany relations.

Any clue where to start ? I noticed that many=true RelatedFields get automatically converted to ManyRelatedFields at https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/relations.py#L76 but afaik, this ManyRelatedField also has the logic to parse the incoming data (POST, PUT, PATCH)...

Anyways, any help is appreciated.

Upvotes: 1

Views: 335

Answers (1)

infinityzxx
infinityzxx

Reputation: 344

So apparently, after a quick chat with @tomchristie, the proper way to do so would be to use the HyperlinkedIdentityField on the serializer.

The relation itself should be applied to the child element with a proper HyperlinkedRelatedField to handle changes to the relation itself as it is the sensible thing to do.

Upvotes: 1

Related Questions