2012-04-04 66 views
0

我正在嘗試在UserResource中添加配置文件數據,但我無法找到實現它的方法。Tastypie - 作爲嵌套資源的配置文件

我調整了幾分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... 

如何解決這個任何想法?

謝謝!

回答

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()