Django datetime migration error

4.9k Views Asked by At

I don't think anything in my model has changed. I have reverted it back to times when it was all fully functional and I still get the following errors.

There are my models:

class UserProfile(models.Model):
    # This line is required. Links UserProfile to a User model instance.
    user = models.OneToOneField(User)

    # The additional attributes we wish to include.
    website = models.URLField(blank=True)
    picture = models.ImageField(upload_to='profile_images', blank=True)


    # Override the __unicode__() method to return out something meaningful!
    def __unicode__(self):
        return self.user.username


# Could create more post classes, or introduce foreign keys. Unsure as of now.
class Post(models.Model):
    title = models.CharField(max_length = 140)
    body = models.TextField()
    date = models.DateTimeField(blank=True)

    INTRODUCTION = 'I'
    STORIES = 'S'
    CATEGORY_CHOICES = (
        (STORIES, 'Stories'),  # Variable name and display value
        (INTRODUCTION, 'Introduce Yourself'),
    )

    category = models.CharField(max_length=1,
                                choices=CATEGORY_CHOICES,
                                default=INTRODUCTION)


    def __unicode__(self):
        return self.title


class Photo(models.Model):
    title = models.CharField(max_length = 140)
    photo = models.ImageField(upload_to='user_images', blank=True, null=True)
    date = models.DateTimeField(blank=True)

    description = models.TextField()

    def __unicode__(self):
        return self.title

Upon running manage.py migrate I am confronted with the following error. Having checked the other answers, it seems it was something to do with datetime being used incorrectly but even if I remove date and the DateTime field altogether the error still persists. Does anyone have any ideas?

Thank very much for your help.

Operations to perform:
  Apply all migrations: contenttypes, admin, sessions, auth, blog
Running migrations:
  Applying blog.0007_auto_20141201_0034...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
    field,
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 160, in add_field
    self._remake_table(model, create_fields=[field])
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 74, in _remake_table
    self.effective_default(field)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/schema.py", line 183, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 627, in get_db_prep_save
    prepared=False)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1286, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1269, in get_prep_value
    value = super(DateTimeField, self).get_prep_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1171, in get_prep_value
    return self.to_python(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1228, in to_python
    parsed = parse_datetime(value)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/utils/dateparse.py", line 70, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or buffer

Here is 'blog.0007...' migration that seems to be failing. To clarify what is going on I was attempting to add a few attributes in order to tell who posted, and at what time etc. Clearly something is not going according to plan..

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('blog', '0006_auto_20141201_0027'),
    ]

    operations = [
        migrations.AddField(
            model_name='post',
            name='created_by',
            field=models.ForeignKey(default=0, related_name='created_by', to=settings.AUTH_USER_MODEL),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='post',
            name='created_on',
            field=models.DateTimeField(default=0, auto_now_add=True),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='post',
            name='edited_by',
            field=models.ForeignKey(default=0, related_name='edited_by', to=settings.AUTH_USER_MODEL),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='post',
            name='edited_on',
            field=models.DateTimeField(default=0, auto_now=True),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='post',
            name='published',
            field=models.BooleanField(default=None),
            preserve_default=True,
        ),
    ]
3

There are 3 best solutions below

1
Anentropic On BEST ANSWER

I think the problem is here:

DateTimeField(default=0...)

Either use None or a datetime object

3
Gautam Bhalla On

Problem seems to be with the unicode method you are using. just try with str() and comment me your status,If it is still causing errors or not.

cheers :-)

0
Kairat Koibagarov On

Delete last migrations file. Then replace date fields on models.DateField(null=True). After you can make migration