Search code examples
pythondjangotastypie

Django tastypie reverse relation not working


I have following CustomerProfile models that OneToOne maps to the User table :

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)

I have created REST API using Tastpye framework with following resource.py to fetch user and its profile details

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']                                                                   

Now what I want is the with a single API call (/user) user will able to fetch its profile details also. Can anybody tell how to do that.

Just for reference I have added following code in UserResource class to achieve this :

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

But I am getting this error :

{"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)

I have surfed a lot to achieve this, but haven't found anything which can achieve the desired output. Please suggest some solution.


Solution

  • CustomerProfile bind to User with one-to-one relationship, so you should to use fields.OneToOneField in UserResource:

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