I recently started using tastypie to open up my api for a potential smartphone app.
I'm using python-2.7 and Django-1.1.2
Two things are confusing me
1: in the EntryResource
class when calling the ForeignKey
, they just call the resource along with the resource_name, when I do it in such a way I get the following traceback.
691. assert isinstance(to, basestring), "%s(%r) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string %r" % (self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT)
Exception Type: AssertionError at /api/v1/activity-stream-item-subject/
Exception Value: ForeignKey(<class 'api.resources.UserResource'>) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'
2: if I change EntryResource
to user = fields.ForeignKey(UserResource, 'user')
, then it runs fine without a traceback, however I cannot see the related information in the JSON response.
api/resources.py
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
filtering = {
'username': ALL,
}
class EntryResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = Entry.objects.all()
resource_name = 'entry'
authorization = Authorization()
filtering = {
'user': ALL_WITH_RELATIONS,
'pub_date': ['exact', 'lt', 'lte', 'gte', 'gt'],
}
Any ideas?
I'm not sure if I understand your first problem, but about the second one, you'll need to specify you want the full resource using:
user = fields.ForeignKey(UserResource, 'user',full=True)
otherwise you'll get the resource URI instead of the contents. (Ref)
Also just for reference, keep in mind that the requirements state:
Django 1.2+ (May work on Django 1.1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With