Skip to content

Instantly share code, notes, and snippets.

@pplante
Created January 15, 2013 02:48
Show Gist options
  • Save pplante/4535616 to your computer and use it in GitHub Desktop.
Save pplante/4535616 to your computer and use it in GitHub Desktop.
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