gregplaysguitar / django-next-prev

Django utility to retrieve the next or previous object, given a current object and a queryset.
Other
25 stars 5 forks source link

prev_in_order() fails. #9

Closed alvindera97 closed 5 years ago

alvindera97 commented 5 years ago

I'm working on a part time project that has a blog section. A section of the blog deals with displaying the next (published) blog post and the previously published blog post - all with respect to the blog post currently viewed.

THIS MEANS THAT IF A CLIENT IS VIEWING POST 3, PREVIOUS_POST == POST 1, NEXT_POST == POST 4

This is the model.py section of the portion pertaining to the BlogPost model.

class BlogPost(models.Model):
    blog_post_author = models.ForeignKey(Author,
                                         null=True,
                                         on_delete=models.CASCADE)
    blog_post_category = models.ForeignKey(BlogCategory,
                                           null=True,
                                           on_delete=models.CASCADE)
    blogpost_title = models.CharField("Title",
                                      max_length=200,
                                      help_text="If necessary, make minimal use of ?s, !s, \\s and :s")
    blog_post_publish_date = models.DateField("Publish Date", blank=True, null=True)

Using the django framework, this is the view section for computing the previous blog post:

all_blog_posts = get_object_or_404(BlogPost, id=id)

all_blog_posts just returns a BlogPost object. The blog post currently viewed.

def previous_post():
    qs = BlogPost.objects.all().order_by('blog_post_publish_date')
    currently_viewed_blog_post = all_blog_posts
    previous_blog_post = prev_in_order(currently_viewed_blog_post, qs=qs)
    return previous_blog_post

next blog post:

def next_post():
    currently_viewed_blog_post = all_blog_posts
    next_blog_post = next_in_order(currently_viewed_blog_post)
    return next_blog_post

The next_post() function works properly, returning the next blog post as designed to. However, the previous_post() function fails and instead returns the first blog post all the time. Even trying the 'prev_in_order()' function separately, displayed the same results i.e. returning only the very first blog post all the time.

This might not (in eventuality) count as an issue but I cannot after several glances find out what is wrong with my code. Thank you. I hope a favorable reply. Cheers.

gregplaysguitar commented 5 years ago

Pretty hard to diagnose without a reproducible example, but you need to make your ordering field non-nullable or results can be inconsistent (see docs: https://github.com/gregplaysguitar/django-next-prev#ordering-considerations)

alvindera97 commented 5 years ago

Perfect! Thank you so much. For future reference purposes, all that needed to be done was to change the ordering field from null=True to null=False. So my model for the blog posts had a little change at the end:

    blog_post_author = models.ForeignKey(Author,
                                         null=True,
                                         on_delete=models.CASCADE)
    blog_post_category = models.ForeignKey(BlogCategory,
                                           null=True,
                                           on_delete=models.CASCADE)
    blogpost_title = models.CharField("Title",
                                      max_length=200,
                                      help_text="If necessary, make minimal use of ?s, !s, \\s and :s")
    blog_post_publish_date = models.DateField("Publish Date", blank=True, null=False)