Created
July 11, 2012 02:18
-
-
Save bohde/3087521 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 MyResource(ModelResource): | |
def full_dehydrate(self, bundle): | |
""" | |
Given a bundle with an object instance, extract the information from it | |
to populate the resource. | |
""" | |
requested = bundle.request.GET.get('fields', '') | |
if requested: | |
requested = set(requested_fields.split(',')) | set(self.fields.keys()) | |
# Dehydrate each field. | |
for field_name, field_object in self.fields.items(): | |
if requested and not field_name in requested: | |
continue | |
# A touch leaky but it makes URI resolution work. | |
if getattr(field_object, 'dehydrated_type', None) == 'related': | |
field_object.api_name = self._meta.api_name | |
field_object.resource_name = self._meta.resource_name | |
bundle.data[field_name] = field_object.dehydrate(bundle) | |
# Check for an optional method to do further dehydration. | |
method = getattr(self, "dehydrate_%s" % field_name, None) | |
if method: | |
bundle.data[field_name] = method(bundle) | |
bundle = self.dehydrate(bundle) | |
return bundle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment