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

RFE: Cleaner hyperlink serialization for dict based APIs #3384

Closed
ncoghlan opened this issue Sep 10, 2015 · 2 comments
Closed

RFE: Cleaner hyperlink serialization for dict based APIs #3384

ncoghlan opened this issue Sep 10, 2015 · 2 comments

Comments

@ncoghlan
Copy link

(Breaking this out of the "Non-Model ViewSets" documentation discussion in #3373 )

I'm working on a project where I'm creating local proxy APIs for existing remote REST services. This means I'm largely working with data stored as attribute dictionaries, rather than as Python objects. While ViewSets + Serializers has mostly "just worked" for that use case, adapting HyperlinkedIdentityField involved completely overwriting get_url():

# Adapts HyperlinkedIdentityField to use __getitem__ rather than __getattr__
class DictBasedIdentityField(serializers.HyperlinkedIdentityField):

    def get_url(self, obj, view_name, request, format):
        # Unsaved objects will not yet have a valid URL.
        try:
            lookup_value = obj[self.lookup_field]
        except KeyError:
            return None
        kwargs = {self.lookup_url_kwarg: lookup_value}
        return self.reverse(view_name, kwargs=kwargs, request=request, format=format)

Would it make sense to have a "get_lookup_value()" method to override?

@tomchristie
Copy link
Member

Would it make sense to have a "get_lookup_value()" method to override?

I think that probably falls into the category of "not worth the extra API surface area"
After all, you'd only be saving two lines of code, and having less indirection here is a benefit to comprehensibility.
Reasonable judgement?

@ncoghlan
Copy link
Author

My other thought was that being able to pass a callable for "lookup_value" would be nice, but DRF doesn't tend to use that API style, so it would be a pretty significant divergence.

While it's still early days, I'm starting to think that there may be scope for a "django-rest-framework-api-proxy" package that adds a few helpers specifically around using DRF as a proxy for other REST APIs. That could also be a potential long term solution to the docs discussion in #3373.

I'll certainly keep considering that possibility as the current project I'm working on continues to evolve.

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

3 participants