1

如何序列化具有 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"
    }
4

0 回答 0