Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:34:46+00:00 2026-05-25T11:34:46+00:00

I’ve got a basic Item model in my Django app: class Item(models.Model): name =

  • 0

I’ve got a basic Item model in my Django app:

class Item(models.Model):
    name = models.CharField(max_length=200)
    brand = models.ForeignKey(User, related_name='items')
    price = models.CharField(max_length=10)
    upload_date = models.DateTimeField(auto_now=False, auto_now_add=True)

For a given item, I am trying to get the next and previous items in the db.

I know I can do this using Django’s built-in Model.get_next_by_FOO(**kwargs), which works perfectly well with the upload_date field, but I need to get the previous and next items given certain OR parameters (for example items that have a certain brand OR are under a certain price).

I can get the previous item using a query like this:

previous_item = Item.objects.filter(id__lt=item.id).filter(Q(brand__in=brands)|Q(price__lt=100))[:1]

My problem is with the next item. Running the same query for the next item results in the most recent item overall that meets the criteria, not the item that comes after the current item in the database. I understand that this is because of the way Django queries the database, but don’t know how to get around it to get the item that comes directly after the current item that also meets the criteria.

Is there a fast, simple way to get the next item for a specific item given multiple OR parameters?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:34:47+00:00Added an answer on May 25, 2026 at 11:34 am

    I didn’t test any of this so might just be talking out my arse, but maybe it’s a starting point?

    Edit: Oh, yes, you were pretty clear about the “OR” issue, but still I missed it. Trying again. The basic idea is to convert your queries into lists and merge the lists. But thinking on that now – I think you can create actual querysets yourself – not that it might be useful in this case? You could do the common heavy lifting of the OR in the function then pass back a queryset which can be further manipulated by the view.

    def get_next(item, brand=None, max_price=None)
        """Returns the next item to the one provided"""
    
        # apply limits to brand/price as specified
        if brand is not None:
             brand_candy = Item.objects.filter(brand=brand)\
                                  .values_list('id','update_date')
        if max_price is not None
             price_candy = Item.objects.filter(price__lt=max_price)\
                                  .values_list('id','update_date')
    
        # arrange all candidates into a joined list
        # TODO: finish this to merge the lists and sort by update_date
        ids = brand_candy.join(price_candy)
    
        # find the location of the current item in the list 
        # TODO: handle not being there
        idx = ids.index(item.id)
    
        # TODO: handle edges of the list
        next_idx = idx + 1
    
        # pluck out the next item
        return Item.objects.get(id=ids[next_idx])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.