Python 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

# Create your models here.

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


# Create your views here.

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 |
    +----+--------+------------+------------+------------+----------------------------+----------------------------+
    """
    # 使用 update 方法更新实例
    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 |
    +----+---------+------------+------------+------------+----------------------------+----------------------------+
    """

    # 使用 update 方法的同时,强制时间更新
    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 |
    +----+--------+------------+------------+------------+----------------------------+----------------------------+
    """

    # 使用 save() 方法更新数据
    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 |
    +----+--------+------------+------------+------------+----------------------------+----------------------------+
    """

    # 修改 mysql 数据库时间设置为本地时区
    # 在 settings.py 下注释:USE_TZ = True
    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')
posted @ 2021-05-09 22:54  f_carey  阅读(23)  评论(0编辑  收藏  举报  来源