django building a get_page_url function in model to return list with pagination number -
i have scenario items paginated list of items in model , want create function return page contains item.
for example
class item(models.model): subject = models.charfield(max_length=200)
assuming have 1000 items in table , return 10 per page. how can build method/function in model tell me in page item fall?
please advise?
you add classmethod takes order_by
, paginate_by
import itertools def grouper(n, iterable, fillvalue=none): args = [iter(iterable)] * n return itertools.izip_longest(fillvalue=fillvalue, *args) class item(models.model): # attrs @classmethod def get_pagination_index(cls, self, order_by, paginate_by): if order_by: qs = cls.objects.all().order_by(order_by) else: qs = cls.objects.all() pages = grouper(paginate_by, qs) = 0 page in pages: += 1 if self in page: return # return item_index = list(qs).index(self) # exact number
Comments
Post a Comment