You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(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?
The text was updated successfully, but these errors were encountered:
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?
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.
(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():
Would it make sense to have a "get_lookup_value()" method to override?
The text was updated successfully, but these errors were encountered: