ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – Django 1.8将datefield更改为DateTimeField

2019-07-02 15:02:12  阅读:271  来源: 互联网

标签:python mysql django datefield


我使用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有一个表’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

Thanx,我读了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

Console写道:

    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 db(使用了mysql-console),没有任何反应.日期仍然是“2015-11-02”如果我添加新数据,在表modified_at和created_at中,日期添加到旧样式(“2015-11-02”).

我究竟做错了什么?

解决方法:

migrate命令仅处理架构迁移,而不处理数据迁移.部分问题是许多数据库后端在同一字段类型中存储日期和日期时间,并且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),
    ]

标签:python,mysql,django,datefield
来源: https://codeday.me/bug/20190702/1357153.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有