Skip to content

Instantly share code, notes, and snippets.

@justinzane
Created May 1, 2012 21:25
Show Gist options
  • Save justinzane/2571533 to your computer and use it in GitHub Desktop.
Save justinzane/2571533 to your computer and use it in GitHub Desktop.
tastypie problem
#models.py
class StoreType(models.Model):
name = models.CharField(max_length=24, unique=True)
class Meta:
ordering = ["name"]
def __unicode__(self):
return ("%24s" % (self.name))
class Store(models.Model):
name = models.CharField(max_length=24)
store_type = models.ForeignKey(StoreType, to_field='name')
class Meta:
ordering = ["store_type", "name"]
unique_together = (("name", "store_type"),)
def __unicode__(self):
return ("%24s (%24s)" % (self.name, self.store_type))
#api.py
class StoreTypeResource(ModelResource):
class Meta:
queryset = StoreType.objects.all()
resource_name = 'storetype'
authorization = Authorization()
class StoreResource(ModelResource):
store_type = fields.ToOneField(StoreTypeResource, 'name', full=True)
class Meta:
queryset = Store.objects.all()
resource_name = 'store'
authorization = Authorization()
##### Error
http://localhost:8000/api/v1/store/?format=json
{"error_message": "'unicode' object has no attribute 'pk'", "traceback": "Traceback (most recent call last):\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 192, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 397, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 427, in dispatch\n response = method(request, **kwargs)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 1037, in get_list\n to_be_serialized['objects'] = [self.full_dehydrate(bundle) for bundle in bundles]\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 654, in full_dehydrate\n bundle.data[field_name] = field_object.dehydrate(bundle)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/fields.py\", line 633, in dehydrate\n return self.dehydrate_related(fk_bundle, self.fk_resource)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/fields.py\", line 514, in dehydrate_related\n return related_resource.full_dehydrate(bundle)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 660, in full_dehydrate\n bundle.data[field_name] = method(bundle)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 798, in dehydrate_resource_uri\n return self.get_resource_uri(bundle)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 1967, in get_resource_uri\n kwargs['pk'] = bundle_or_obj.obj.pk\n\nAttributeError: 'unicode' object has no attribute 'pk'\n"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment