{"id":520,"date":"2014-09-17T11:00:34","date_gmt":"2014-09-17T02:00:34","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=520"},"modified":"2014-12-03T09:57:07","modified_gmt":"2014-12-03T00:57:07","slug":"django-datetimefield-default-migration","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2014\/09\/django-datetimefield-default-migration\/","title":{"rendered":"Django 1.7 \u3067 DateTimeField \u306b\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u4ed8\u304d\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u8a2d\u5b9a\u3057\u3066 makemigration \u3067\u304d\u306a\u3044"},"content":{"rendered":"

Django 1.7 \u304b\u3089\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30b9\u30c6\u30e0\u304c\u7d44\u307f\u8fbc\u307e\u308c\u307e\u3057\u305f<\/a>. \u3057\u304b\u3057, \u3053\u306e\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30b9\u30c6\u30e0\u3067\u30e2\u30c7\u30eb\u306e DateTimeField \u306b\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u4ed8\u304d\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u3068 ValueError \u4f8b\u5916\u304c\u9001\u51fa\u3055\u308c\u307e\u3059.<\/p>\n

<\/p>\n

\u554f\u984c\u767a\u751f\u306e\u624b\u9806<\/h2>\n

\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3068\u3057\u307e\u3059. \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306b\u306f\u305d\u308c\u305e\u308c\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u4ed8\u304d\u306e\u73fe\u5728\u6642\u523b\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059. django.utils.timezone.now \u306f settings.USE_TZ = True \u306e\u5834\u5408\u306b\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u4ed8\u304d\u306b\u306a\u308a\u307e\u3059.<\/p>\n

\r\nfrom datetime import datetime\r\n\r\nfrom django.db import models\r\nfrom django.utils.timezone import now, utc\r\n\r\n\r\nclass SampleModel(models.Model):\r\n    added = models.DateTimeField(default=datetime.utcnow().replace(tzinfo=utc))\r\n    updated = models.DateTimeField(default=now())\r\n<\/pre>\n

\u4e0a\u306e\u30e2\u30c7\u30eb\u306b\u3064\u3044\u3066 makemigration \u3092\u5b9f\u884c\u3059\u308b\u3068 ValueError \u304c\u9001\u51fa\u3055\u308c\u307e\u3059.<\/p>\n

\r\n$ python manage.py makemigrations\r\nMigrations for 'app':\r\n  0001_initial.py:\r\n    - Create model SampleModel\r\nTraceback (most recent call last):\r\n  File "manage.py", line 10, in <module>\r\n    execute_from_command_line(sys.argv)\r\n\r\n\u301c\u301c\u301c \u7701\u7565 \u301c\u301c\u301c\r\n\r\n  File "**********\/venv\/lib\/python3.4\/site-packages\/django\/db\/migrations\/writer.py", line 271, in serialize\r\n    raise ValueError("Cannot serialize datetime values with timezones. Either use a callable value for default or remove the timezone.")\r\nValueError: Cannot serialize datetime values with timezones. Either use a callable value for default or remove the timezone.\r\n<\/pre>\n

\u554f\u984c\u306e\u4fee\u6b63\u65b9\u6cd5<\/h2>\n

\u554f\u984c\u306e\u4fee\u6b63\u65b9\u6cd5\u306f ValueError \u4f8b\u5916\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u306b\u8a18\u8ff0\u3055\u308c\u3066\u3044\u307e\u3059. (Either use a callable value for default or remove the timezone.) \u524d\u8005\u306f\u547c\u3073\u51fa\u3057\u53ef\u80fd\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 (\u95a2\u6570\u306a\u3069) \u3092\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306b\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5\u3067, \u5f8c\u8005\u306f\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u306e\u306a\u3044 datetime \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8a2d\u5b9a\u3059\u308b\u3068\u3044\u3046\u3082\u306e\u3067\u3059. \u5f8c\u8005\u3092\u63a1\u7528\u3059\u308b\u306e\u306f\u3042\u307e\u308a\u597d\u307e\u3057\u304f\u306a\u3044\u30b1\u30fc\u30b9\u304c\u591a\u3044\u3068\u601d\u3046\u307e\u3059\u304c, \u4fee\u6b63\u3057\u305f\u30e2\u30c7\u30eb\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n

\r\nfrom datetime import datetime\r\n\r\nfrom django.db import models\r\nfrom django.utils.timezone import now\r\n\r\n\r\nclass SampleModel(models.Model):\r\n    added = models.DateTimeField(default=datetime.utcnow())\r\n    updated = models.DateTimeField(default=now)\r\n<\/pre>\n

updated \u306e\u65b9\u306f\u524d\u8005\u306e\u65b9\u6cd5\u3092\u7528\u3044, \u95a2\u6570\u3092\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059. added \u306e\u65b9\u306f datetime.datetime.utcnow \u304c tzinfo = None (\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u306e\u306a\u3044) \u306e datetime \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059\u3053\u3068\u3092\u7528\u3044\u3066\u3044\u307e\u3059.<\/p>\n

\u53c2\u8003<\/h2>\n