'Manager'对象是Django分页上的不可取消的错误

时间:2012-12-05 20:03:33

标签: python django django-pagination

在Django 1.4.2中,我使用了一个简单的分页代码,如given in official documentation

...
paginator = Paginator(songs, 25) # Show 25 songs per page

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

当我运行该视图时,我在songs = paginator.page(1)行上显示“ '经理'对象无法替代 ”错误。我搜索错误,但我找不到任何解决方案。有什么问题?

修改

songs是django模型对象列表。全视图代码就是这样:

def index(request):
    songs = Song.objects

    #filter params
    q_name = request.GET.get('name', None)
    if q_name:
        songs = songs.filter(name__contains=q_name)

    q_composer = request.GET.get('composer', None)
    if q_composer:
        songs = songs.filter(composer__name__contains=q_composer)

    q_composer_id = request.GET.get('composer_id', '')
    if q_composer_id != '':
        songs = songs.filter(composer__id=q_composer_id)

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

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

    return render(request, 'index.html', {'songs': songs})

1 个答案:

答案 0 :(得分:1)

没有提供过滤器时会发生什么。然后,您尝试对AuditManger进行分页,而不是查询集。您应该将第一个查询更改为:

songs = Song.objects.all()