如何将具有外键的模型序列化为Django auth用户,以便可以检索用户名?这是我通过Tastypie尝试过的:
def dehydrate(self, bundle):
posts = Post.objects.annotate(num_poster = Count('likedby')).order_by('-num_poster')
specificpost = posts.get(id__exact=bundle.data['id'])
likedList = specificpost.likedby.all()[:7]
liked_data = serializers.serialize('python', likedList, fields=('user__username', 'userPic'), use_natural_keys=True)
liked_actual = [d['fields'] for d in liked_data]
bundle.data['likepreview'] = liked_actual
return bundle
在我得到的JSON中,我可以看到userPic字段,但没有用户名字段,为什么会这样?
{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 17
},
"objects": [{
"id": "19",
"liked": 0,
"likepreview": [{
"userPic": "http://s3.amazonaws.com/post/mike/profile/photo/mikeProfilePic.jpg"
}