Django==1.11
djangorestframework==3.7.3
支持YAML docstrings
支持python3
给正在运行的Docker容器动态绑定卷组(动态添加Volume)
subprocess.Popen 实时获取docker events事件()
django和celery的低耦合使用
基于类的celery任务定义
通过 {domain}/{uri}?s={size}实现获取指定大小缩略图
原图:localhost/u/1523562/avatar
缩略图:localhost/u/1523562/avatar?s=200
缩略图:localhost/u/1523562/avatar?s=100
打印调用日志:打出所有的调用请求、参数和返回值,或写入日志
检测文件改变自动重启: autoreload, 更新代码不用重启服务,使用werkzeug的run_with_reloader函数实现
from logger import get_logger
# 普通控制台输出日志
logger = get_logger(name='console')
# 普通控制台和按日期周期文件输出日志
logger = get_logger(name='sanic', use_rotating=True)
output: 2018-09-09 11:04:43 - your_logger_name - [main.py:7] INFO : Log info
根据id倒序(即创建时间倒叙)分页,适用简单场景
使用max获取最大id,从最后一条数据向前分页
通过id倒序向前推进使用 <=
和 limit
结合的方式优化分页加载
eg: select * from yourtable where id <= seek_id order by id desc limit 20;
from pagination.pagination import WholeTableIdReversePagination
queryset = YourModel.objects.all()
pagination = WholeTableIdReversePagination()
result = list(pagination.paginate_queryset(queryset, request))
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'pagination.pagination.WholeTableIdReversePagination',
}
GET https://api.example.org/accounts/?page_seek_id=100
or
GET https://api.example.org/accounts/
HTTP 200 OK
{
"count": 20
"next": "https://api.example.org/accounts/?page_seek_id=80",
"previous": none,
"results": [
…
]
}
from pagination.utils import QueryWrapper
sql = 'select id, username, first_name from auth_user'
queryset = QueryWrapper(sql)
count = queryset.count()
data = queryset.all()
# 在Django中使用
from django.core.paginator import Paginator
pages = Paginator(queryset, per_page=10)
page = pages.page(page_no) # 获取某页数据
# 在django rest framework中使用
page = self.paginate_queryset(queryset)
results = self.get_paginated_response(page).data
print(results)
>>>
{
"count": 25,
"next": "http://127.0.0.1:8888/test/?page=2",
"previous": null,
"results": [{
"id": 11349230,
"username": "张三",
"phone": "1440182340944",
}, {
"id": 11344204,
"username": "李四",
"phone": "1440182333431",
},..
}