Tastypie - 作为嵌套资源的配置文件

时间:2012-04-04 09:51:51

标签: django tastypie

我正在尝试在UserResource中添加配置文件数据,但我找不到这样做的方法。

我稍微调整了docs上的代码,就像这样:

class UserResource(ModelResource):
    profile = fields.ForeignKey(UserProfile, 'profile', full=True)

    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']

        #authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

    def dispatch(self, request_type, request, **kwargs):

        kwargs['profile'] = request.user.get_profile()
        return super(UserResource, self).dispatch(request_type, request, **kwargs)

但是给了我这个错误:

error_message: Cannot resolve keyword 'profile' into field. Choices are: date_joined, email, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, password...

如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

首先,我要确保您使用的是ProfileResource,而不仅仅是模型。第二个User.profile解析为个人资料。所以可能是这样的:

class UserResource(ModelResource):
    profile = fields.ForeignKey('myapp.api.resources.ProfileResource', 'profile', full=True)

    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']

        #authentication = BasicAuthentication()
        authorization = DjangoAuthorization()