Created
January 15, 2013 02:48
-
-
Save pplante/4535616 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
from tastypie import fields | |
from tastypie.bundle import Bundle | |
class FastDehydratableForeignKey(fields.ForeignKey): | |
def dehydrate(self, bundle): | |
if isinstance(bundle, Bundle): | |
other_resource = self.to_class() | |
if hasattr(other_resource, '_meta') and other_resource._meta.object_class: | |
fk_field = bundle.obj._meta.get_field(self.attribute) | |
db_column = fk_field.db_column or '%s_id' % self.attribute | |
if hasattr(bundle.obj, db_column): | |
foreign_key = getattr(bundle.obj, db_column) | |
return other_resource.get_resource_uri(other_resource._meta.object_class(pk=foreign_key)) | |
return super(FastDehydratableForeignKey, self).dehydrate(bundle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment