Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for custom hyperlinked related field #731

Open
hmpf opened this issue Nov 11, 2019 · 5 comments
Open

Support for custom hyperlinked related field #731

hmpf opened this issue Nov 11, 2019 · 5 comments

Comments

@hmpf
Copy link

hmpf commented Nov 11, 2019

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/"
    }
  }
}
@sliverc
Copy link
Member

sliverc commented Nov 14, 2019

Could SerializerMethodResourceRelatedField be what you are looking for? Otherwise in the same file linked above there are many other examples.

@hmpf
Copy link
Author

hmpf commented Nov 14, 2019

SerializerMethodResourceRelatedField assumes a model and a view name. SerializerMethodField does not assume anything except getting an obj passed in.

@sliverc
Copy link
Member

sliverc commented Nov 14, 2019

I am not 100% certain but when looking at the code it should work without passing on a model. However I am not sure why you do not want to pass on a view name. Are you trying to link to a different service than DJA is running on?

@hmpf
Copy link
Author

hmpf commented Nov 14, 2019

Yep. My service has info and metadata about other services. There's also worklng around/hiding some (SQL) modelling details.

@sliverc
Copy link
Member

sliverc commented Nov 14, 2019

No this is currently not possible. I had a quick look at the renderer and I do not see a quick workaround either to override the url of a relationship from the client code.

A new field would need to be created and the renderer changed to be aware of it.

@sliverc sliverc changed the title Including link to a related non-model resource Allow related resource links to point at external service Nov 14, 2019
@sliverc sliverc changed the title Allow related resource links to point at external service Support for custom hyperlinked related field Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants