TastyPie:相关资源 returns NULL
TastyPie: related resource returns NULL
我有扩展 Django 的 User
模型的 UserProfile 模型:
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 是正确的,因为我的 UserResource
中有一个属性 null=True
。响应为空,这就是我收到 NULL 的原因。但是我该如何解决这个问题呢?怎么了?
可能是项目结构的问题?资源文件是:
<project_name>/api/api.py
无意中找到了决定。型号:
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"
}
}
我有扩展 Django 的 User
模型的 UserProfile 模型:
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 是正确的,因为我的 UserResource
中有一个属性 null=True
。响应为空,这就是我收到 NULL 的原因。但是我该如何解决这个问题呢?怎么了?
可能是项目结构的问题?资源文件是:
<project_name>/api/api.py
无意中找到了决定。型号:
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"
}
}