django-pagination

Paginate relationship in Django REST Framework?

可紊 提交于 2019-11-29 11:46:16
问题 We are using Django REST Framework for our API and we have a need to paginate relationship fields that return multiple items. To demonstrate using examples similar to those in the documentation: class TrackSerializer(serializers.ModelSerializer): class Meta: model = Track fields = ('order', 'title') class AlbumSerializer(serializers.ModelSerializer): tracks = TrackSerializer(many=True) class Meta: model = Album fields = ('album_name', 'artist', 'tracks') Example serialized output for an Album

Pagination in Django-Rest-Framework using API-View

自闭症网瘾萝莉.ら 提交于 2019-11-29 01:44:55
I currently have an API view setup as follows: class CartView(APIView): authentication_classes = [SessionAuthentication, TokenAuthentication] permission_classes = [IsAuthenticated, ] api_view = ['GET', 'POST'] def get(self, request, format=None): try: cart = request.user.cart except Cart.DoesNotExist: cart = Cart.objects.create(user=request.user) cart_details = cart.cart_details.all() serializer = CartDetailSerializer(cart_details, many=True, fields=['id', 'item', 'quantity', 'product_type']) return Response(serializer.data) Here CartDetailSerializer is a normal ModelSerializer. I want to

Django pagination…slicing pages to show fraction of total pages?

左心房为你撑大大i 提交于 2019-11-29 01:06:25
问题 I have the pagination module working for the most part but there's one issue. How can I only show a slice of the total available pages. For example, let's say I'm on page 5 of n pages, I'd want to show. 1,2,3,4,5,6....(n-1)(n). I believe that Ruby has some fancy front-end magic to accomplish this. Is there anything that I can do within the django module? From looking at other sites, it seems as though the logic basically goes, pick a fixed number of spots. If that number is larger than the

Display only some of the page numbers by django pagination

痴心易碎 提交于 2019-11-28 15:15:53
问题 I am using the django paginator in the template. Its working ok, but not good when there's large numbers of pages. views.py: def blog(request): blogs_list = Blog.objects.all() paginator = Paginator(blogs_list, 1) try: page = int(request.GET.get('page', '1')) except: page = 1 try: blogs = paginator.page(page) except(EmptyPage, InvalidPage): blogs = paginator.page(page) return render(request, 'blogs.html', { 'blogs':blogs }) snippet of the template: <div class="prev_next"> {% if blogs.has