How to continue the object in django get_next/previous_by_date_created and published?
<p>Anyone can help me how to continue the object in Django by <code>get_next/previous_by_date_created</code> and published only?</p>

<pre><code>&gt;&gt;&gt; obj_4 = Post.objects.get(pk=4)
&gt;&gt;&gt; obj_4.publish
True
&gt;&gt;&gt; obj_4.get_previous_by_date_created()
&lt;Post: Title Post 3&gt;
&gt;&gt;&gt; obj_4.get_previous_by_date_created().publish
False
&gt;&gt;&gt; obj_4.get_previous_by_date_created().pk
3
&gt;&gt;&gt; obj_2 = Post.objects.get(pk=2)
&lt;Post: Title Post 2&gt;
&gt;&gt;&gt; obj_2.publish
True
&gt;&gt;&gt; obj_4.get_next_by_date_created()
&lt;Post: Title Post 5&gt;
&gt;&gt;&gt; obj_4.get_next_by_date_created().publish
True
&gt;&gt;&gt;
</code></pre>

<p>I need to continue the object by published only. For example:</p>

<pre><code>&gt;&gt;&gt; obj_4 = Post.objects.get(pk=4)
&gt;&gt;&gt; obj_4.get_previous_by_date_created_and_published()
&lt;Post: Title Post 2&gt;
</code></pre>

<p>Other conditions, if we have such as bellow:</p>

<pre><code>pk      : [1,    2,     3,     4,     5,    6,    7,     8   ]
publish : [True, False, False, False, True, True, False, True]
</code></pre>

<p>And then, if i starting from <code>pk=5</code>, the output should be <code>pk=1</code>.</p>

<pre><code>&gt;&gt;&gt; obj_5 = Post.objects.get(pk=5)
&gt;&gt;&gt; obj_5.get_previous_by_date_created_and_published().pk
1
&gt;&gt;&gt;
&gt;&gt;&gt; obj_5.get_previous_by_date_created_and_published().publish
True
&gt;&gt;&gt;
</code></pre>

<p>Thanks so much before...</p>

<p><strong>UPDATE SOLVED:</strong></p>

<p><strong>1.</strong> <code>views.py</code></p>

<pre><code>class DetailPostView(generic.DetailView):
    model = Post
    template_name = 'detail.html'

    def get_previous_by_created_and_published(self):
        prev = Post.objects.filter(
            created__lte=self.get_object().created,
            publish=True
        ).order_by('-created').first()
        return prev

    def get_next_by_created_and_published(self):
        next = Post.objects.filter(
            created__gte=self.get_object().created,
            publish=True
        ).order_by('-created').first()
        return next

    def get_context_data(self, **kwargs):
        context_data = super(DetailPostView, self).get_context_data(**kwargs)
        context_data[
            'get_previous_by_created_and_published'
        ] = self.get_previous_by_created_and_published()
        context_data[
            'get_next_by_created_and_published'
        ] = self.get_next_by_created_and_published()
        return context_data
</code></pre>

<p><strong>2.</strong> <code>templates/detail.html</code></p>

<pre><code>&lt;ul class="pager"&gt;
  &lt;li class="previous"&gt;
  {% if get_previous_by_created_and_published %}
    &lt;a href="{% url 'detail_post_page' slug=get_previous_by_created_and_published.slug %}"&gt;&amp;larr; Previous&lt;/a&gt;
  {% endif %}
  &lt;/li&gt;
  &lt;li class="next"&gt;
  {% if get_next_by_created_and_published %}
    &lt;a href="{% url 'detail_post_page' slug=get_next_by_created_and_published.slug %}"&gt;Next &amp;rarr;&lt;/a&gt;
  {% endif %}
  &lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>An implementation which will work is:</p>

<pre><code>def get_previous_by_created_and_published(self):
    prev = Post.objects.filter(created__lte=self.created,
                               published=True
                               ).order_by('-created').first()
    return prev
</code></pre>

<p>Though I'm assuming here that if there are multiple published posts created before this then you'll only want the most recent, as the name of your method suggests.</p>

<p><code>get_next_by_created_and_published</code> will work in a similar manner, though with <code>__gte</code> and the ordering flipped.</p>

python django django-templates