【问题标题】:Django tastypie reverse relation not workingDjango sweetpie反向关系不起作用
【发布时间】:2015-03-16 11:15:57
【问题描述】:

我有以下 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)

我已经浏览了很多来实现这一点,但还没有找到任何可以实现所需输出的东西。请提出一些解决方案。

【问题讨论】:

    标签: python django tastypie


    【解决方案1】:

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

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

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

    但是如果有人能够清楚地阐明资源映射和反向映射,那对所有人来说都会很有帮助,显然 django 官方文档对我帮助不大。

    谢谢

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-07
        • 1970-01-01
        • 1970-01-01
        • 2014-05-27
        • 1970-01-01
        • 2010-11-16
        • 2012-04-19
        相关资源
        最近更新 更多