ansheng’s blog!

Django ORM获取指定天数据的几种方式

环境准备

假设你已经创建了虚拟环境并安装好了Django

# 系统是Ubuntu
$ uname -a
Linux Ubuntu 4.10.0-19-generic #21-Ubuntu SMP Thu Apr 6 17:04:57 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
# 我用的是django 1.11.2版的
$ django-admin --version
1.11.2
$ django-admin startproject d
$ python manage.py startapp db
# - db/models.py
from django.db import models


# Create your models here.

class Language(models.Model):
    name = models.CharField(max_length=15)
    add_time = models.DateTimeField()
INSTALLED_APPS = [
    ...
    'db',
]
$ python manage.py makemigrations
$ python manage.py migrate
# python manage.py shell
from db.models import Language
from django.utils import timezone
from datetime import timedelta,date
current_date=timezone.now()  # 当前时间 2017-06-04 19:48:30
Language.objects.create(name='Python',add_time=current_date - timedelta(days=1))
Language.objects.create(name='C',add_time=current_date - timedelta(days=1))
Language.objects.create(name='C++',add_time=current_date - timedelta(days=2))
Language.objects.create(name='Java',add_time=current_date - timedelta(days=2))
Language.objects.create(name='Go',add_time=current_date - timedelta(days=3))
Language.objects.create(name='JS',add_time=current_date - timedelta(days=3))

实操

以下的操作都是为了获取2017-06-03的数据,过滤出来的结果应该都是下面的内容

<QuerySet [{'id': 1, 'add_time': datetime.datetime(2017, 6, 3, 11, 17, 31, 848500, tzinfo=<UTC>), 'name': 'Python'}, {'id': 2, 'add_time': datetime.datetime(2017,
6, 3, 11, 17, 31, 848500, tzinfo=<UTC>), 'name': 'C'}]>

过滤年月日字符串是否存在

Language.objects.filter(add_time__contains=date(2017, 6, 3)).values()

是否以指定年月日开头

Language.objects.filter(add_time__startswith=date(2017, 6, 3)).values()

制定年月日

Language.objects.filter(add_time__year=2017,add_time__month=6,add_time__day=3).values()

date是在django1.9.x新增的

Language.objects.filter(add_time__date=date(2017, 6, 3)).values()

制定一个日期的范围

Language.objects.filter(add_time__range=(current_date, current_date + timedelta(days=1))).values()

stackoverflow也有一篇比较好的回答:

When USE_TZ is True, fields are converted to the current time zone before filtering.