LoginSignup
0
0

More than 1 year has passed since last update.

【Django】models.pyでデフォルト値を作成時刻にする方法

Posted at

よく作成時間のカラムを作成するのに、
デフォルト値を作成時間にすることがあるかと思います。
その際に発生したWARNINGメッセージについて記載します。

各バージョン

  • Django: 4.0
  • Python: 3.10.2

WARNING発生時のmodels.py

デフォルト値を作成時間にした際のmodels.py

models.py
from django.db import models
from datetime import datetime
# Create your models here.

class Todo(models.Model):
  title = models.CharField(max_length=100)
  description = models.TextField(blank=True, null=True)
  create_time = models.DateTimeField(default=datetime.now())
  update_time = models.DateTimeField(default=datetime.now())
  limit_date = models.DateField(null=False)
  status = models.BooleanField(default=False)

datetimeを使用してnowで作成された時間をcreate_timeカラムとupdate_timeカラムに入れようとします。

WARNINGメッセージ

python manage.py makemigrationsを実行した時のログは以下です

WARNINGS:
todo.Todo.create_time: (fields.W161) Fixed default value provided.
	HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
todo.Todo.update_time: (fields.W161) Fixed default value provided.
	HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`

翻訳してみると以下です

todo.Todo.create_time: (fields.W161) 固定されたデフォルト値が提供されています。
HINT: このフィールドにはこれは、あなたが望むものではない可能性があります。もし現在の日付をデフォルトにしたいのなら、 `django.utils.timezone.now` を使ってください。
todo.Todo.update_time: (fields.W161) デフォルト値が提供されるように修正されました。
	HINT: このフィールドには、固定された日付/時間/日時の値をデフォルトとして設定しているようです。これは、あなたが望むものではない可能性があります。現在の日付をデフォルトにしたい場合は、 `django.utils.timezone.now` を使ってください。

もし現在の日付をデフォルトにしたいのなら、 django.utils.timezone.now を使ってください。

まさしくここですね

models.py修正

以下に修正します

models.py
from django.db import models
from django.utils import timezone
# Create your models here.

class Todo(models.Model):
  title = models.CharField(max_length=100)
  description = models.TextField(blank=True, null=True)
  create_time = models.DateTimeField(default=timezone.now())
  update_time = models.DateTimeField(default=timezone.now())
  limit_date = models.DateField(null=False)
  status = models.BooleanField(default=False)

リトライ

python manage.py makemigrationsをリトライすると同じWARNINGメッセージが、、、

再修正

timezone.now()の()を削除します

models.py
from django.db import models
from django.utils import timezone
# Create your models here.

class Todo(models.Model):
  title = models.CharField(max_length=100)
  description = models.TextField(blank=True, null=True)
  create_time = models.DateTimeField(default=timezone.now)
  update_time = models.DateTimeField(default=timezone.now)
  limit_date = models.DateField(null=False)
  status = models.BooleanField(default=False)

リトライ

python manage.py makemigrationsをリトライするとWARNINGが消えました

Migrations for 'todo':
  todo/migrations/0003_alter_todo_create_time_alter_todo_update_time.py
    - Alter field create_time on todo
    - Alter field update_time on todo
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0