Django models.py 中的 auto_now和auto_now_add的区别
1. 验证环境:mysql 环境下
1.1 配置数据库环境为 mysql 数据库环境
1. 修改 setting.py 如下
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': 'datedb',
'USER': 'root',
'PASSWORD': 'toor',
}
}
2. 创建 mysql 数据库
mysql> CREATE DATABASE `datedb` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION='N' */;
3. 在项目配置文件夹下的 __init__.py 文件中,配置 Django 使用 pymysql 模块连接 MySQL 数据库:
import pymysql
pymysql.install_as_MySQLdb()
1.2 app01/models.py 下创建新表对象
from django.db import models
class TimeTest(models.Model):
user = models.CharField(max_length=32)
time1 = models.DateField(default='2021-12-12')
time2 = models.DateField(auto_now=True)
time3 = models.DateField(auto_now_add=True)
time4 = models.DateTimeField(auto_now=True)
time5 = models.DateTimeField(auto_now_add=True)
"""
auto_now=Ture,修改时间,但要注意使用 save() 方法才会修改当前数据的更新时间,使用 update() 方法不会修改,
auto_now_add=True,创建时间。
"""
python manage.py makemigrations
python manage.py migrate
1.3 app01/urls.py 下创建新路由
from django.urls import path
from app01 import views
urlpatterns = [
path('index/',views.index,),
]
1.4 app01/views.py 下创建新视图
from django.shortcuts import render, HttpResponse
from app01.models import TimeTest
def index(request):
TimeTest.objects.create(
user='user01'
)
"""
+----+--------+------------+------------+------------+----------------------------+----------------------------+
| id | user | time1 | time2 | time3 | time4 | time5 |
+----+--------+------------+------------+------------+----------------------------+----------------------------+
| 1 | user01 | 2021-12-12 | 2021-05-09 | 2021-05-09 | 2021-05-09 14:18:19.179750 | 2021-05-09 14:18:19.179750 |
+----+--------+------------+------------+------------+----------------------------+----------------------------+
"""
TimeTest.objects.filter(user='user01').update(user='user02')
"""
由以下结果可知,在 mysql 数据库中,使用 update 方法更新数据不会触发日期更新。
+----+---------+------------+------------+------------+----------------------------+----------------------------+
| id | user | time1 | time2 | time3 | time4 | time5 |
+----+---------+------------+------------+------------+----------------------------+----------------------------+
| 1 | user02 | 2021-12-12 | 2021-05-09 | 2021-05-09 | 2021-05-09 14:18:19.179750 | 2021-05-09 14:18:19.179750 |
+----+---------+------------+------------+------------+----------------------------+----------------------------+
"""
import datetime
new_date = datetime.datetime.now()
TimeTest.objects.filter(user='user02').update(user='user03',time4=new_date)
"""
+----+--------+------------+------------+------------+----------------------------+----------------------------+
| id | user | time1 | time2 | time3 | time4 | time5 |
+----+--------+------------+------------+------------+----------------------------+----------------------------+
| 1 | user03 | 2021-12-12 | 2021-05-09 | 2021-05-09 | 2021-05-09 22:25:43.361058 | 2021-05-09 14:18:19.179750 |
+----+--------+------------+------------+------------+----------------------------+----------------------------+
"""
ret = TimeTest.objects.filter(user='user03').first()
ret.user = 'user04'
ret.save()
"""
+----+--------+------------+------------+------------+----------------------------+----------------------------+
| id | user | time1 | time2 | time3 | time4 | time5 |
+----+--------+------------+------------+------------+----------------------------+----------------------------+
| 1 | user04 | 2021-12-12 | 2021-05-09 | 2021-05-09 | 2021-05-09 14:29:46.715723 | 2021-05-09 14:18:19.179750 |
+----+--------+------------+------------+------------+----------------------------+----------------------------+
"""
TimeTest.objects.create(
user='user05'
)
"""
+----+--------+------------+------------+------------+----------------------------+----------------------------+
| id | user | time1 | time2 | time3 | time4 | time5 |
+----+--------+------------+------------+------------+----------------------------+----------------------------+
| 1 | user04 | 2021-12-12 | 2021-05-09 | 2021-05-09 | 2021-05-09 14:29:46.715723 | 2021-05-09 14:18:19.179750 |
| 2 | user05 | 2021-12-12 | 2021-05-09 | 2021-05-09 | 2021-05-09 22:34:53.446463 | 2021-05-09 22:34:53.446463 |
+----+--------+------------+------------+------------+----------------------------+----------------------------+
"""
return HttpResponse('ok')