Skip to content

Support for custom hyperlinked related field #731

Open
@hmpf

Description

@hmpf

I need to be able to add links to read-only resources into the relationships-object of a resource as easily as adding a non-link field with SerializerMethodField. (Generating the link with SerializerMethodField is trivially easy, but they are links.) In all cases so far I can generate the link from data in the object.

Is this supposed to be possible at all or is it just poorly documented?

I've tried non-model serializers, ResourceRelatedField, HyperlinkedRelatedField, and I'm now at the stage where the code is running under pdb in an attempt to figure out how things are supposed to work. It seems such a straightforward, obvious thing to do.

I'm talking about things like this:

class Some(Model):
  data = models.CharField()

class SomeSerializer(serializers.ModelSerializer):
   extra = serializers.SomethingMagicField()

  class Meta:
    model = Some
    fields = ['data','extra']

  def extra(self, obj):
    return 'other_url/{}/'.format(obj.pk)

leading to

{
  "data": {
    "type": "Some",
    "id": "1",
    "attributes": {
       "data": "foo",
    },
    "relationships": {
      "extra": {
        "links": {
          "related": "other_url/1/"
        }
      }
    }
  }
}

instead of

{
  "data": {
    "type": "Some",
    "id": "1",
    "attributes": {
       "data": "foo",
       "extra": "other_url/1/"
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions