搞清楚datetime.datetime和datetime.date模块
他们两个的格式区别
datetime模块
In [1]: from datetime import datetime
In [2]: datetime.now()
Out[2]: datetime.datetime(2018, 11, 19, 9, 30, 34, 795738)
In [3]: print(datetime.now())
2018-11-19 09:30:40.604522
date模块
In [1]: import datetime
In [5]: print(datetime.date(2018,11,11))
2018-11-11
django里datetime.datetime和datetime.date模块显示
from datetime import datetime
class People(models.Model):
name = models.CharField(max_length=30)
age = models.CharField(max_length=30)
part = models.CharField(max_length=30)
add_time = models.DateTimeField(default=datetime.now)
date = models.DateField(auto_now_add=True)
def __str__(self):
return self.name
库里存储
2018-11-19 09:24:13.531194
后台展示
admin.py
from .models import People
class PeopleAdmin(admin.ModelAdmin):
list_display = ['name', 'age', 'part', 'add_time', 'date']
# list_filter = ['name', 'age', 'part']
# search_fields = ['name', 'age', 'part']
admin.site.register(People, PeopleAdmin)
datetime.date字段sql
发现对models.DateField的字段过滤很好用
In [25]: People.objects.filter(date__day=19).query.__str__()
Out[25]: 'SELECT `app03_people`.`id`, `app03_people`.`name`, `app03_people`.`age`, `app03_people`.`part`, `app03_people`.`add_time`, `app03_people`.`date` FROM `app03_people` WHERE EXTRACT(DAY FROM `app03_people`.`date`) = 19'
参考: https://www.django.cn/article/show-15.html?tdsourcetag=s_pcqq_aiomsg
# 匹配日期,date
User.objects.filter(create_time__date=datetime.date(2018, 8, 1))
User.objects.filter(create_time__date__gt=datetime.date(2018, 8, 2))
# 匹配年,year
User.objects.filter(create_time__year=2018)
User.objects.filter(create_time__year__gte=2018)
# 匹配月,month
User.objects.filter(create_time__month__gt=7)
User.objects.filter(create_time__month__gte=7)
# 匹配日,day
User.objects.filter(create_time__day=8)
User.objects.filter(create_time__day__gte=8)
# 匹配周,week_day
User.objects.filter(create_time__week_day=2)
User.objects.filter(create_time__week_day__gte=2)
# 匹配时,hour
User.objects.filter(create_time__hour=9)
User.objects.filter(create_time__hour__gte=9)
# 匹配分,minute
User.objects.filter(create_time__minute=15)
User.objects.filter(create_time__minute_gt=15)
# 匹配秒,second
User.objects.filter(create_time__second=15)
User.objects.filter(create_time__second__gte=15)
# 按天统计归档
today = datetime.date.today()
select = {'day': connection.ops.date_trunc_sql('day', 'create_time')}
deploy_date_count = Task.objects.filter(
create_time__range=(today - datetime.timedelta(days=7), today)
).extra(select=select).values('day').annotate(number=Count('id'))
Q 的使用
models.DateTimeField的过滤
auto_now_add 自动添加,以添加记录时日期
auto_add 每次更新记录,自动更新
库里我想存储models.DateTimeField类型, 不想存储models.DateField类型, 因为time还需要展示的
方法1:
前端获取date, 过滤后返回
People.objects.filter(add_time__lt = datetime.datetime(2018,11,19))
- 但是每次会提示这个
E:\learn\learndj\venv\lib\site-packages\django\db\models\fields\__init__.py:1421: RuntimeWarning: DateTimeField People.add_time received a naive datetime (2018-11-19 00:00:00) while time zone support is active.
RuntimeWarning)
settings里时区设置
1.要么不设置
2.要么设置成
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
db里均存储的是当前系统时间.(不会差8h的.)