1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# create by oldman
# Date: 2019/8/6
from django.db.models import Q
from qa.models import Question
from talos.rpc import bind_context
@bind_context('qa/choices')
def topic_choices(ctx, q='', page=1, num=30, initial=None):
page = int(page)
num = int(num)
if initial is not None:
if isinstance(initial, (list, tuple)):
qry = Q(id__in=initial)
else:
qry = Q(id=initial)
else:
qry = Q(id__contains=q) | Q(title__contains=q)
query = Question.objects.filter(qry)
# total_count = query.count()
total_count = 0
start_pos = (page - 1) * num
start_pos = start_pos if start_pos >= 0 else 0
results = [
{
'id': obj.id,
'text': u'{}:{}'.format(obj.id, obj.title),
} for obj in query[start_pos: start_pos + num]
]
return {'total_count': total_count, 'results': results, 'page': page, 'num': num}