[Fixed]-Django retrieve all rows from a table but the last three

1👍

This should work,

Book.objects.order_by('-id')[3:]

if you want to get them in order

Book.objects.order_by('-id')[3:][::-1]
👤dnit13

0👍

As you have

books = Book.objects.order_by('-id')[:3]

list_to_exclude = [b.id for b in books]

and then

books_excl = Book.objects.all().exclude(id__in=list_to_exclude)

0👍

Use:

Book.objects.exclude(pk__in=Book.objects.order_by('-id')[:3])

Note: above query tested on postgres 9.5.3

Leave a comment