ApP*_*PeL 6 python api django json
我最近开始使用tastypie为一个潜在的智能手机应用程序打开我的api.
我正在使用python-2.7和Django-1.1.2
有两件事令我困惑
1:在EntryResource类中调用时ForeignKey,它们只是调用资源和resource_name,当我这样做时,我得到以下回溯.
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'
Run Code Online (Sandbox Code Playgroud)
2:如果我EntryResource改为user = fields.ForeignKey(UserResource, 'user'),那么它运行正常而没有回溯,但是我无法在JSON响应中看到相关信息.
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'],
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我不确定我是否理解你的第一个问题,但关于第二个问题,你需要指定你想要使用的完整资源:
user = fields.ForeignKey(UserResource, 'user',full=True)
Run Code Online (Sandbox Code Playgroud)
否则你将获得资源URI而不是内容.(参考)
也仅供参考,请记住要求状态:
Django 1.2+(可以在Django 1.1上工作)
| 归档时间: |
|
| 查看次数: |
3711 次 |
| 最近记录: |