Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
How to get the next n records in a django queryset?

Is there a way to get the next n records in django queryset?

For example, how would I get the next 5 records after getting the first five records? My use case is that I have a see more button on my page and I want to load the next 5 records whenever the see more button is clicked (via ajax)

I would like to do something like this

#function that handles ajax
def load_next_five(initial_five)
    next_five = initial_five.???
    return JsonResponse({'next_five': list(next_five)})

def main_view(request):
    initial_five = Car.objects.filter(year__gte=2000)
    ...

How would I go about the load_next_five function?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

use a slice. You'll have to pass in where you want to start.

def load_next_five(start)
    next_five = Car.objects.filter(year__gte=2000)[start:start+5]
    return JsonResponse({'next_five': list(next_five)})

def main_view(request):
    initial_five = Car.objects.filter(year__gte=2000)[:5]

Under the covers, Django will turn that into clauses that will limit the results on the database side (for example, LIMIT and OFFSET).

over 4 years ago · Santiago Trujillo Denunciar

0

Follow the django paginator, easy to use.

def load_next_five(request):
    car_obj = Car.objects.all()
    paginator = Paginator(car_obj, 5) # change this number as you required
    page = request.GET.get('page')

    try:
        cars = paginator.page(page)
    except PageNotAnInteger:
        cars = paginator.page(1)
    except EmptyPage:
        cars = paginator.page(paginator.num_pages)

    return JsonResponse({'cars': cars})
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda