Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
django101
Commits
cc988a2f
Commit
cc988a2f
authored
May 31, 2024
by
Administrator
Browse files
fixed issue of timezone.now()
parent
5ac71371
Changes
1
Show whitespace changes
Inline
Side-by-side
my_app/models.py
View file @
cc988a2f
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
# Feel free to rename the models, but don't rename db_table values or field names.
from
django.db
import
models
from
django.db
import
models
from
django.utils
import
timezone
class
DemoUsers
(
models
.
Model
):
class
DemoUsers
(
models
.
Model
):
...
@@ -34,7 +35,7 @@ class DemoUsers(models.Model):
...
@@ -34,7 +35,7 @@ class DemoUsers(models.Model):
# 检查对象是否存在数据库中,即判断是否有主键pk
# 检查对象是否存在数据库中,即判断是否有主键pk
if
self
.
pk
:
# 对象已存在,表示是更新操作
if
self
.
pk
:
# 对象已存在,表示是更新操作
# 在这里执行你希望在更新时发生的自动赋值逻辑
# 在这里执行你希望在更新时发生的自动赋值逻辑
self
.
last_update_date
=
now
()
# 你的自定义更新逻辑
self
.
last_update_date
=
timezone
.
now
()
# 你的自定义更新逻辑
else
:
else
:
self
.
creation_date
=
ne
w
()
self
.
creation_date
=
timezone
.
no
w
()
super
().
save
(
*
args
,
**
kwargs
)
# 调用父类的save方法完成保存
super
().
save
(
*
args
,
**
kwargs
)
# 调用父类的save方法完成保存
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment