Skip to content

Instantly share code, notes, and snippets.

@MRHarrison
Forked from santiagobasulto/gist:2897326
Last active May 26, 2016 10:18
Show Gist options
  • Save MRHarrison/c810c27d3c5259a3b4e7 to your computer and use it in GitHub Desktop.
Save MRHarrison/c810c27d3c5259a3b4e7 to your computer and use it in GitHub Desktop.
""" This is a quick fix to provide full dehydration by demand
on Django Tastypie.
In order to use it you can query for:
/api/v1/user/1/related_full=True
"""
class RelatedResource(ModelResource):
user = fields.ToOneField(UserResource, 'user')
class Meta:
queryset = Related.objects.all()
resource_name = 'related'
class UserResource(ModelResource):
related = fields.ToOneField(RelatedResource, 'related', full=False) # Default is false
class Meta:
queryset = User.objects.all()
resource_name = 'user'
def dehydrate(self, bundle):
for key, value in bundle.request.GET.iteritems():
show_full = False
relation_key = str()
if '_full' in key:
relation_key = key.split('_full')[0]
show_full = value.lower() == "true"
if show_full:
field = getattr(self, relation_key)
old_full = field.full
field.full = True
bundle.data[relation_key] = field.dehydrate(bundle)
field.full = old_full
return bundle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment