from django.db.models.functions import Concat
from django.db.models import Value
defconcat_function():
user = User.objects.filter(open_id='test_open_id').annotate(
screenname=Concat(
Value('openid='),'open_id',
Value(','),
Value('nickname='),'nickname'))[0]print(user.screenname)
字符串长度
from django.db.models.functions import Length
deflength_function():
user = User.objects.filter(open_id='test_open_id').annotate(
open_id_length=Length('open_id'))[0]print(user.open_id_length)
字符串大小写转换
from django.db.models.functions import Upper,Lower
defcase_function():
user = User.objects.filter(open_id='test_open_id').annotate(
open_id_Upper=Upper('open_id'),
open_id_Lower=Lower('open_id'))[0]print(user.open_id_Upper,' ',user.open_id_Lower)
日期Now
from apis.models import App
from django.db.models.functions import Now
defnow_function():
apps = App.objects.filter(publish_date__lte=Now())for app in apps:print(app)
时间截断Trunc
from django.db.models.functions import Trunc
from django.db.models import Count
deftrunc_function():
app_per_day = App.objects.annotate(
publish_day=Trunc('publish_date','day')).values('publish_day').annotate(
publish_num=Count('appid'))for app in app_per_day:print('date:', app['publish_day'],', publish num:', app['publish_num'])