0

嗨,我需要从两个表中获取数据,外键中第一个的 Id 用于其他

这是我的代码

def allplayers(request):
    cursor = connection.cursor()
    player_list = myplayer.objects.all()
    get_player_vital=[]
    for i in player_list:
        get_player_vital = [vitals.objects.filter(playerID =i.id )]

    paginator = Paginator(player_list, 25) # Show 25 contacts per page

    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        contacts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        contacts = paginator.page(paginator.num_pages)

    return render_to_response('allplayers.html', {"contacts": contacts,'get_player_vital':get_player_vital})

当我在模板中打印 get_player_vital 时,它只返回最后一条记录

4

1 回答 1

0

你需要这样做,我猜:

def allplayers(request):
    player_list = myplayer.objects.all()
    get_player_vital = [vitals.objects.filter(playerID__in=player_list.values_list('id', flat=True))]
   ...
   return render_to_response('allplayers.html', {"contacts": contacts,'get_player_vital':get_player_vital})

因为您在list每个迭代循环中创建新对象,而不是仅包含一个对象。

于 2013-08-16T06:21:16.930 回答