Skip to content

Instantly share code, notes, and snippets.

@andyreagan
Last active June 23, 2020 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyreagan/b8109f1f5dd6ccb2b1dd3fc6b29da80d to your computer and use it in GitHub Desktop.
Save andyreagan/b8109f1f5dd6ccb2b1dd3fc6b29da80d to your computer and use it in GitHub Desktop.
Smart url encoder for tastypie
from django.utils.html import smart_urlquote
from tastypie import fields
class SmartUrlField(fields.ApiField):
"""
A field for urls in plain text.
"""
dehydrated_type = 'string'
help_text = 'a field for urls in plain text.'
def convert(self, value):
if value is None:
return None
return smart_urlquote(value)
def hydrate(self, bundle):
value = super(SmartUrlField, self).hydrate(bundle)
return self.convert(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment