Django 1.8 change date to DateTimeField

I am using Django 1.8

I have model.py, like this

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 has a table "created_at" and "modified_at", which has data like "2015-11-02", I would like to change the data to "2015-11-02 14:54:22"

I am changing models.py to this

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)

Then I run the migrations in the console

python manage.py makemigrations
python manage.py migrate

But nothing happened. Data in MySQL tables does not change, and when I add new information, time is added to MySQL in old templates "2015-11-02"

How do I change them?

PS before Lego Stormtroopr

Thanx, I read the Django tutorial and wrote this code

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),
     ]

After that I launched

   python manage.py migrate

The console wrote this:

    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-), . - "2015-11-02" , modified_at created_at, ( "2015-11-02" ).

?

+4
1

migrate , . , , Django .

, .

, :

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),
    ]
+2

Source: https://habr.com/ru/post/1615150/


All Articles