A
A
Alexander Leyba2019-09-13 13:50:24
Django
Alexander Leyba, 2019-09-13 13:50:24

Negative indexing is not supported. how to loop through and output the last 8 elements of a QuerySet list?

Good afternoon, I ran into a problem, I need to display the last 8 articles with the "Architecture" tag, but when I try to slice the list, I get Negative indexing is not supported.

tag_arch = TagPlaceModel.objects.get(name='Архитектура').placemodel_set.all()[:-8]

I use django template and tried to cut in the template but it gives the same error.
Also tried:
tag_arch = TagPlaceModel.objects.get(name='Архитектура').placemodel_set.all()[:-8]
            a = list(tag_arch)[:-8]

in this case teg_arch returns an empty list.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Leyba, 2019-09-13
@Alex_shady

in the model

class Meta:
        verbose_name = 'Новость'
        verbose_name_plural = 'Новости'
        ordering = ['-id']

in view
tag_arch = TagPlaceModel.objects.get(name='Архитектура').placemodel_set.all()[0:8]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question