Created
June 8, 2012 18:06
-
-
Save santiagobasulto/2897326 to your computer and use it in GitHub Desktop.
Quick hack for turning full=True on related fields on Tastypie
This file contains 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
""" 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/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): | |
show_full = bundle.request.GET.get('full', "false").lower() == "true" | |
if show_full: | |
old_full = self.related.full | |
self.related.full = True | |
bundle.data['related'] = self.related.dehydrate(bundle) | |
self.related.full = old_full | |
return bundle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment