Django 1.8 将日期字段更改为 DateTimeField
Django 1.8 change datefield to DateTimeField
我使用 Django 1.8
我有model.py,像这样
class Product(models.Model):
name = models.CharField(max_length=150)
created_at = models.DateField(auto_now_add = True)
modified_at = models.DateField(auto_now = True)
MySQL 有一个 table 'created_at' 和 'modified_at' 有这样的数据 "2015-11-02"
我想将数据更改为“2015-11-02 14:54:22”
我把models.py改成这个
class Product(models.Model):
name = models.CharField(max_length=150)
created_at = models.DateTimeField(auto_now_add = True)
modified_at = models.DateTimeField(auto_now = True)
然后我 运行 在控制台中迁移
python manage.py makemigrations
python manage.py migrate
但是什么也没发生。 MySQL 表中的数据没有变化,当我添加新信息时,在 MySQL 中以旧模式添加时间“2015-11-02”
如何更改它们?
PS 到 Lego Stormtroopr
谢谢,我看了 Django 教程,写了这段代码
from __future__ import unicode_literals
from django.db import migrations, models
import datetime
def add_time_to_date(apps, schema_editor):
datetables = apps.get_model("product", "Product")
delta = datetime.timedelta(hours=1)
for x in datetables.objects.all():
x.created_at = x.created_at + delta
x.modified_at = x.modified_at + delta
x.save()
class Migration(migrations.Migration):
dependencies = [
('product', '0003_auto_20151110_1726'),
]
operations = [
migrations.RunPython(add_time_to_date),
]
之后,我运行
python manage.py migrate
控制台写道:
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, product, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying product.0004_auto_20151110_1821.../home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1414: RuntimeWarning: DateTimeField Product.created_at received a naive datetime (2015-11-02 00:00:00) while time zone support is active.
RuntimeWarning)
/home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/backends/mysql/base.py:124: Warning: Data truncated for column 'modified_at' at row 1
return self.cursor.execute(query, args)
/home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1414: RuntimeWarning: DateTimeField Product.created_at received a naive datetime (2015-11-08 00:00:00) while time zone support is active.
RuntimeWarning)
/home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1414: RuntimeWarning: DateTimeField Product.created_at received a naive datetime (2015-11-09 00:00:00) while time zone support is active.
RuntimeWarning)
/home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1414: RuntimeWarning: DateTimeField Product.created_at received a naive datetime (10 00:00:00) while time zone support is active.
RuntimeWarning)
OK
我检查了 Mysql 数据库(使用了 mysql-console),但没有任何反应。日期仍然是“2015-11-02” 如果我添加新数据,在 tables modified_at 和 created_at 中,日期添加到旧样式(“2015-11-02”)。
我做错了什么?
migrate
命令仅处理 schema 迁移,不处理数据迁移。部分问题是许多数据库后端将日期和日期时间存储在相同的字段类型中,是 Django 将它们强制转换为正确的类型。
所以要做到这一点,你还需要做一个 data migration。
您的迁移代码看起来像这样,但这取决于添加无时间日期的时间:
from django.db import models, migrations
def date_to_datetime(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
Product = apps.get_model("yourappname", "Product")
for product in Product.objects.all():
# convert a date to a date time on the product instance
product.save()
class Migration(migrations.Migration):
dependencies = [
('yourappname', 'migration_name'),
]
operations = [
migrations.RunPython(date_to_datetime),
]
我使用 Django 1.8
我有model.py,像这样
class Product(models.Model):
name = models.CharField(max_length=150)
created_at = models.DateField(auto_now_add = True)
modified_at = models.DateField(auto_now = True)
MySQL 有一个 table 'created_at' 和 'modified_at' 有这样的数据 "2015-11-02" 我想将数据更改为“2015-11-02 14:54:22”
我把models.py改成这个
class Product(models.Model):
name = models.CharField(max_length=150)
created_at = models.DateTimeField(auto_now_add = True)
modified_at = models.DateTimeField(auto_now = True)
然后我 运行 在控制台中迁移
python manage.py makemigrations
python manage.py migrate
但是什么也没发生。 MySQL 表中的数据没有变化,当我添加新信息时,在 MySQL 中以旧模式添加时间“2015-11-02”
如何更改它们?
PS 到 Lego Stormtroopr
谢谢,我看了 Django 教程,写了这段代码
from __future__ import unicode_literals
from django.db import migrations, models
import datetime
def add_time_to_date(apps, schema_editor):
datetables = apps.get_model("product", "Product")
delta = datetime.timedelta(hours=1)
for x in datetables.objects.all():
x.created_at = x.created_at + delta
x.modified_at = x.modified_at + delta
x.save()
class Migration(migrations.Migration):
dependencies = [
('product', '0003_auto_20151110_1726'),
]
operations = [
migrations.RunPython(add_time_to_date),
]
之后,我运行
python manage.py migrate
控制台写道:
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, product, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying product.0004_auto_20151110_1821.../home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1414: RuntimeWarning: DateTimeField Product.created_at received a naive datetime (2015-11-02 00:00:00) while time zone support is active.
RuntimeWarning)
/home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/backends/mysql/base.py:124: Warning: Data truncated for column 'modified_at' at row 1
return self.cursor.execute(query, args)
/home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1414: RuntimeWarning: DateTimeField Product.created_at received a naive datetime (2015-11-08 00:00:00) while time zone support is active.
RuntimeWarning)
/home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1414: RuntimeWarning: DateTimeField Product.created_at received a naive datetime (2015-11-09 00:00:00) while time zone support is active.
RuntimeWarning)
/home/djangojunior2/virtualpython2/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1414: RuntimeWarning: DateTimeField Product.created_at received a naive datetime (10 00:00:00) while time zone support is active.
RuntimeWarning)
OK
我检查了 Mysql 数据库(使用了 mysql-console),但没有任何反应。日期仍然是“2015-11-02” 如果我添加新数据,在 tables modified_at 和 created_at 中,日期添加到旧样式(“2015-11-02”)。
我做错了什么?
migrate
命令仅处理 schema 迁移,不处理数据迁移。部分问题是许多数据库后端将日期和日期时间存储在相同的字段类型中,是 Django 将它们强制转换为正确的类型。
所以要做到这一点,你还需要做一个 data migration。
您的迁移代码看起来像这样,但这取决于添加无时间日期的时间:
from django.db import models, migrations
def date_to_datetime(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
Product = apps.get_model("yourappname", "Product")
for product in Product.objects.all():
# convert a date to a date time on the product instance
product.save()
class Migration(migrations.Migration):
dependencies = [
('yourappname', 'migration_name'),
]
operations = [
migrations.RunPython(date_to_datetime),
]