-
-
Save MRHarrison/c810c27d3c5259a3b4e7 to your computer and use it in GitHub Desktop.
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/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