2017-01-14 9 views
0

たとえば、ThreadCommentのコンセプトを実装したフォーラムを構築したいとします。CommentモデルはForeignKeyからThreadです。Django:ページネーションで特定のオブジェクトを見つけることは可能ですか?

class Thread(models.Model): 
    title = models.CharField(max_length=200) 
    .... 

class Comment(models.Model): 
    thread = models.ForeignKey(Thread, ...) 
    comment = models.TextField() 
    .... 
  1. 詳細スレッドは、ページネーションのため/?page={number}を使用して、いくつかのコメント
  2. 詳細スレッドでコメントを持っています。

現在、私のコメントページネーションはうまく機能しています。しかし、私はコメントの単一のオブジェクトを探したい場合には問題があります。

たとえば、page=3にはid=13というコメントが1つあります。ここで

thread

は私views.py

def thread_detail(request, topic_slug, pk): 
    """ 
    detail thread public view. 
    :param `topic_slug` is slug from the topic. 
    :param `pk` is id from thread. 
    """ 
    template_name = 'public/thread_detail.html' 

    numb_pages = 10 
    get_page = request.GET.get('page') 
    thread = get_object_or_404(Thread, topic__slug=topic_slug, pk=pk) 
    comments = Comment.objects.filter(thread=thread).order_by('created') 
    .... 

    page_obj = PaginatorFBV(comments, numb_pages, get_page).queryset_paginated() 
    page_range = PaginatorFBV(comments, numb_pages, get_page).page_numbering() 
    comments = page_obj.object_list 

    context = { 
     'thread': thread, 
     'comments': comments, 
     'page_obj': page_obj, 
     'page_range': page_range 
    } 
    return render(request, template_name, context) 

である私は私のfind_comment_view.py、例えば、動的でそれを作成したい:

def find_comment(request, pk): 
    """ 
    Return redirect to the comment on thread, with current page. 
    :param `pk` is pk/id from comment. 
    """ 
    comment = get_object_or_404(Comment, pk=pk) 
    comments = Comment.objects.filter(thread__in=comment.thread) 

    # what i should be do here? 

本当にありがとうございました前..

更新:

私はこの1つを試してみるとうまくいく。 しかし、の場合、ページ数が多い場合は別の問題が見つかりました。ページの千...もちろん、それは長い時間がかかるだろう...

def find_comment(request, pk): 
    """ 
    Return redirect to the comment on thread, with current page. 
    :param `pk` is pk/id from comment. 
    """ 
    comment = get_object_or_404(Comment, pk=pk) 
    comments = Comment.objects.filter(thread=comment.thread).order_by('created') 
    thread = get_object_or_404(Thread, pk=comment.thread.pk) 

    # Initial `page_obj` to get max numb of pagination. 
    items_per_page = 10 # this is same with what we do for `numb_pages = 10` inside `def thread_detail(...)` 
    initial_page_obj = PaginatorFBV(comments, items_per_page, 1).queryset_paginated() 
    max_numb_pages = initial_page_obj.paginator.num_pages 

    for page in range(max_numb_pages): 
     page += 1 
     page_obj = PaginatorFBV(comments, items_per_page, page).queryset_paginated() 
     comment_objects = page_obj.object_list 

     # Check the output of `comment_objects` 
     # >>> print(comment_objects, page) 
     # <QuerySet [<Comment: regina>]> 1 
     # <QuerySet [<Comment: beverly>]> 2 
     # <QuerySet [<Comment: amy>]> 3 
     # <QuerySet [<Comment: hannah>]> 4 

     for obj in comment_objects: 
      if obj.pk == comment.pk: 
       # eg: /thread/20/?page=2#comment-30 
       return redirect(
        ('%s?page=%s#comment-%s') % (
         thread.get_absolute_url(), 
         page, comment.pk 
        ) 
       ) 
    return redirect(('%s#comment-%s') % (thread.get_absolute_url(), comment.pk)) 

urls.py

urlpatterns = [ 
    .... 
    url(
     r'^comment/direct/(?P<pk>[-\d]+)/$', 
     find_comment, name='find_comment_page' 
    ), 
] 

答えて

0

の内側にあなたは次に

position=Comment.objects.filter(created__lt=comment.created).order_by('created').count() 

を経由して、オブジェクトの位置を取得することができますページ番号を取得する

items_per_page=10 
page = int(position/items_per_page) 
+0

ここで_position_と_page_の違いは何ですか? –

+0

'position'はクエリセット内のオブジェクトの位置です。 1つのページのコメントなしで分割すると、オブジェクトが表示されるページが表示され、 'page'変数の意味は – Bitonator

+0

です。私はさまざまな方法でそれを試しましたが、私は多くのページがある場合、別の問題が見つかりました。ページ数千...もちろんそれは長い時間がかかるだろう...私はすべき最善の方法は何ですか?私の最新の更新をチェックアウトしてください... –

関連する問題