Created
July 9, 2014 17:28
-
-
Save maryokhin/8f94c102c5c0e19ba86a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReverseField(Field): | |
""" | |
Simple read only field for returning reverse URLs. | |
This field utilizes information from the object being serialized. It does | |
not care about what the current URL contains, lookup fields in the URL, | |
nor the structure of a model. Instead, it takes the field names passed | |
into lookup_fields and reads the values of those fields from the object. | |
The values are then passed as arguments to the reverse function, which | |
constructs the final URL. | |
This field should be used instead of HyperlinkedRelatedField because it | |
does not depend on URL keywords. It is not always possible to know which | |
keyword arguments were used for finding the object. It may also be | |
necessary to use more than a single keyword when constructing the URL. | |
Reverse field currently does not support traversals past the current | |
object. However, this functionality would not be difficult to add. See | |
KeyFilterBackend for an example of how to traverse fields. | |
""" | |
def __init__(self, view_name, lookup_fields=('pk',), **kwargs): | |
self.view_name = view_name | |
self.lookup_fields = lookup_fields | |
super(ReverseField, self).__init__(**kwargs) | |
def field_to_native(self, obj, field_name): | |
# There is no primary key if the object has not yet been saved. | |
# Generating the URL becomes impossible if we need the key before | |
# it is available. Thus, we resign to our fate and return None. | |
if 'pk' in self.lookup_fields and getattr(obj, 'pk', None) is None: | |
return None | |
request = self.context.get('request', None) | |
if request is None: | |
raise KeyError("ReverseField requires the request object to be present in the serializer context.") | |
args = [getattr(obj, lookup_field) for lookup_field in self.lookup_fields] | |
return reverse(self.view_name, args=args, request=request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment