title: Django: is possible to find specific object in pagination?
body: <p>Let say, I want to built a forum with implements the <code>Thread</code> and <code>Comment</code> concept.. The <code>Comment</code> model is <code>ForeignKey</code> to the <code>Thread</code>.</p>
  
  <pre><code>class Thread(models.Model):
      title = models.CharField(max_length=200)
      ....
  
  class Comment(models.Model):
      thread = models.ForeignKey(Thread, ...)
      comment = models.TextField()
      ....
  </code></pre>
  
  <ol>
  <li>The <strong><em>Detail Thread</em></strong> have some <strong><em>Comments</em></strong></li>
  <li>The <strong><em>Comments at Detail Thread</em></strong> using <code>/?page={number}</code> for the pagination.</li>
  </ol>
  
  <p>Currently, my comments pagination is work well. But, I have a problem if I want to find the single object of comment.</p>
  
  <p>For example, I have single object of comment with <code>id=13</code> found in the <code>page=3</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/nBpOb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nBpOb.png" alt="thread"></a></p>
  
  <p>Here is my <code>views.py</code></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I want to create it with dynamically, for example in my <code>find_comment_view.py</code>:</p>
  
  <pre><code>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?
  </code></pre>
  
  <p>Thanks so much before..</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I try with this one, and work well for me. <strong><em>But</em></strong>, I found another problem if has many pages, eg; thousand of pages.. of course it will take a long time...</p>
  
  <pre><code>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`
          # &gt;&gt;&gt; print(comment_objects, page)
          # &lt;QuerySet [&lt;Comment: regina&gt;]&gt; 1
          # &lt;QuerySet [&lt;Comment: beverly&gt;]&gt; 2
          # &lt;QuerySet [&lt;Comment: amy&gt;]&gt; 3
          # &lt;QuerySet [&lt;Comment: hannah&gt;]&gt; 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))
  </code></pre>
  
  <p>and inside <code>urls.py</code></p>
  
  <pre><code>urlpatterns = [
      ....
      url(
          r'^comment/direct/(?P&lt;pk&gt;[-\d]+)/$',
          find_comment, name='find_comment_page'
      ),
  ]
  </code></pre>