TastyPie:相关资源返回NULL

时间:2016-03-12 13:05:44

标签: django tastypie

我有UserProfile模型,它扩展了Django的User模型:

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name="profile")
    avatar = models.CharField(max_length=40, default='0')
    activation_key = models.CharField(max_length=40, blank=True)
    key_expires = models.DateTimeField(default=django.utils.timezone.now)

    def __str__(self):
        return self.user.username

    class Meta:
        verbose_name_plural = u'User profiles'

我有两个资源:

class UserProfileResource(ModelResource):
    class Meta:
        queryset = UserProfile.objects.all()
        resource_name = 'profile'
        allowed_methods = ['get']

class UserResource(ModelResource):
    profile = fields.ToOneField('api.api.UserProfileResource',
                                'profile', full=True, null=True)
    class Meta:
        queryset = User.objects.all()
        #fields = ['first_name', 'last_name', 'email', 'date_joined', 'profile']
        fields = [ 'profile' ]
        allowed_methods = ['get', 'post']
        resource_name = 'user'
        authentication = SessionAuthentication()
        authorization = DjangoAuthorization()
        detail_uri_name = 'username'

    def obj_create(self, bundle, **kwargs):
        return super(UserResource, self).obj_create(bundle, user=bundle.request.user)

    def authorized_read_list(self, object_list, bundle):
        return object_list.filter(user=bundle.request.user)

我尝试发出GET请求:

http://localhost:8000/api/v1/user/<some_username>

我收到了:

Object { profile: null, resource_uri: "/api/v1/user/<some_username>/" }

配置文件为NULL是正确的,因为我的null=True中有一个属性UserResource。响应为空,这就是我收到NULL的原因。但我怎么能解决这个问题呢?有什么问题?

也许是因为项目的结构?资源文件是:

<project_name>/api/api.py

1 个答案:

答案 0 :(得分:0)

无意中我找到了决定。型号:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    avatar = models.CharField(max_length=40, default='0')
    activation_key = models.CharField(max_length=40, blank=True)
    key_expires = models.DateTimeField(default=django.utils.timezone.now)

    def __str__(self):
        return self.user.username

    class Meta:
        verbose_name_plural = u'User profiles'

资源:

class UserProfileResource(ModelResource):

    class Meta:
        queryset = UserProfile.objects.all()
        authentication = SessionAuthentication()
        authorization = DjangoAuthorization()
        allowed_methods = ['get', 'post']
        resource_name = 'profile'
        include_resource_uri = False

class UserResource(ModelResource):
    userprofile = fields.ToOneField(UserProfileResource, 'userprofile', null=True, full=True)

    class Meta:
        queryset = User.objects.all()
        fields = ['first_name', 'last_name', 'email', 'date_joined', 'userprofile']
        allowed_methods = ['get', 'post']
        resource_name = 'user'
        detail_uri_name = 'username'
        authentication = SessionAuthentication()
        authorization = DjangoAuthorization()

如您所见,这条线非常重要:

userprofile = fields.ToOneField(UserProfileResource, 'userprofile', null=True, full=True)

此属性的名称(userprofile)应该等于模型的名称(UserProfile)。

GET请求:

http://localhost:8000/api/v1/user/<some_username>

响应:

{
    "date_joined": "2016-03-01T08:22:40", 
    "email": "test@test.test", 
    "first_name": "Ivan", 
    "last_name": "Ivan", 
    "resource_uri": "/api/v1/user/<some_username>/", 
    "userprofile": {
        "activation_key": "...", 
        "avatar": "...", 
        "id": 22, 
        "key_expires": "2016-03-14T17:11:01"
    }
}