0

我正在尝试在 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...

知道如何解决这个问题吗?

谢谢!

4

1 回答 1

0

首先,我会确保您使用的是 aProfileResource而不仅仅是模型。第二个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()
于 2012-04-04T11:50:19.363 回答