【问题标题】:Tastypie - Profile as Nested ResourceTastypie - 配置文件作为嵌套资源
【发布时间】:2012-04-04 09:51:51
【问题描述】:

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

我对@9​​87654321@ 上的代码稍作调整,如下所示:

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...

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

谢谢!

【问题讨论】:

    标签: django tastypie


    【解决方案1】:

    首先,我会确保您使用的是 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 2016-09-19
      • 2012-03-05
      • 1970-01-01
      相关资源
      最近更新 更多