django 백엔드 페이지

6704 단어
백엔드 페이지 나누기 함수
from django.core.paginator import Paginator, EmptyPage
import datetime
from django.core import serializers
import json

from django.db import models


class BizManager(models.Manager):
    def get_by_natural_key(self, id):
        return self.get(id=id)


class Biz(models.Model):
    objects = BizManager()
    name = models.CharField(u'  ', max_length=32)
    updated_date = models.DateTimeField(u'    ', auto_now=True)

    class Meta:
        verbose_name = u"  "
        verbose_name_plural = u"  "

    def natural_key(self):
        return {"id": self.id,"name": self.name}  #       


def page_to(Qset, page, page_size, fields=()):
    #       
    count = Qset.count()
    paginator = Paginator(Qset, page_size)
    try:
        data_ = paginator.page(page)
    except EmptyPage:
        data_ = paginator.page(paginator.num_pages)
    if fields:
        data = serializers.serialize('json', data_, fields=fields, use_natural_foreign_keys=True)
    else:
        data = serializers.serialize('json', data_, use_natural_foreign_keys=True)
    data = json.loads(data)
    return (count, data)
``

좋은 웹페이지 즐겨찾기