Django tastypie反向关系不起作用

时间:2015-03-16 11:15:57

标签: python django tastypie

我有以下OneToOne映射到User表的CustomerProfile模型:

class CustomerProfile(models.Model):
'''Profile details of the customers. All extra details are mentioned        
here.'''
    user = models.OneToOneField(User, related_name='profile')
    phone = models.CharField(max_length=200, null=True, blank=True)

    class Meta:
        app_label = 'testapp'

    def __unicode__(self):
    return unicode(self.user)

我使用Tastpye框架创建了REST API,并使用以下resource.py来获取用户及其配置文件详细信息

class UserResource(ModelResource):                                                                  
'''Fetch user details'''                                                                        

    class Meta:                                                                                     
        queryset = User.objects.all()                                                               
        resource_name = 'user'                                                                      
        include_resource_uri = False                                                                
        allowed_methods = ['get']                                                                   
        excludes = ['password','last_login','is_superuser','is_staff','date_joined']                
        filtering = {                                                                               
            'id': ['exact'],                                                                        
            'username': ['exact']                                                                   
        }                                                                                           

class CustomerProfileResource(ModelResource):                                                       
'''Fetch customer details and all coupons'''                                                    

    class Meta:                                                                                     
        queryset = CustomerProfile.objects.all()                                                    
        resource_name = 'customer'                                                                  
        include_resource_uri = False                                                                
        allowed_methods = ['get']                                                                   

现在我想要的是使用单个API调用(/ user)用户也可以获取其个人资料详细信息。任何人都可以告诉你如何做到这一点。

仅供参考,我在UserResource类中添加了以下代码来实现此目的:

profile = fields.ToManyField('coin.api.CustomerProfileResource', 'profile', null=True, full=True)

但是我收到了这个错误:

{"error_message": "'CustomerProfile' object has no attribute 'all'", "traceback": "Traceback (most recent call last):\n\n  File \"/home/rajeev/projects/bitbucket/coin/env/local/lib/python2.7/site-packages/tastypie/resources.py\", line 195, in wrapper\n    response = callback(request, *args, **kwargs)

为了达到这个目的,我已经做了很多工作,但是没有找到能达到预期效果的东西。请提出一些解决方案。

2 个答案:

答案 0 :(得分:1)

幸运的是我通过点击和试用得到了答案:)

由于OneToOne映射将CustomerProfile资源映射到UserResouce,因此我们必须使用fields.ToOneField而不是fields.ToManyField,同时从UserResource执行反向关系,如下所示:

profile = fields.ToOneField('coin.api.CustomerProfileResource', 'profile', null=True, full=True)

但是如果有人能够清楚地阐明资源映射和反向映射,这对所有人都非常有帮助,显然django官方文档无法帮助我很多。

由于

答案 1 :(得分:0)

CustomerProfile以一对一的关系绑定到User,因此您应该在UserResource中使用fields.OneToOneField:

class UserResource(ModelResource):                                                                  
    '''Fetch user details'''                                                                        
    custom_profile = fields.OneToOneField(CustomerProfileResource, 'custom_profile', related_name='profile', full=True)

    class Meta:
        fields = ['id', 'username', 'custom_profile']  # other fields what you need
        ...