[FIXED] "django.core.exceptions.ValidationError" error

Issue

I am writing a simple game in Django, all of things were right, but suddenly..., I was encountered by following error:

  • Django.v = 1.7
  • Python.v = 3.4

I don't know what is wrong with these codes:

    (test)alireza@alireza:~/test/test1$ python manage.py syncdb
    Operations to perform:
      Synchronize unmigrated apps: django_admin_bootstrapped, django_admin_bootstrapped_bootstrap3, crispy_forms
      Apply all migrations: contenttypes, admin, auth, arosis, sessions
    Synchronizing apps without migrations:
      Creating tables...
      Installing custom SQL...
      Installing indexes...
    Running migrations:
      Applying arosis.0008_auto_20150212_0826...Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/home/alireza/test/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
        utility.execute()
      File "/home/alireza/test/lib/python3.4/site-
...
...
...

        return self.to_python(value)
      File "/home/alireza/test/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1252, in to_python
        params={'value': value},
    django.core.exceptions.ValidationError: ["'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

In my models.py:

class Move(models.Model):
    """docstring for Move"""
    x = models.IntegerField()
    y = models.IntegerField()
    comment = models.CharField(max_length=30)
    game = models.ForeignKey(Game)
    by_first_player = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "{}".format(self.comment)

    class Meta:
        get_latest_by = 'timestamp'

    def player(self):
        return self.game.first_player if self.by_first_player else self.game.second_player

I gave auto_now_add=True,

but at first, when I run :

python manage.py makemigrations

It asked me for entering a default value for DateTimeField()

What should I do?

0008_auto_20150212_0826.py

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

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('arosis', '0007_auto_20150211_1844'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='move',
            options={'get_latest_by': 'timestamp'},
        ),
        migrations.AddField(
            model_name='move',
            name='by_first_player',
            field=models.BooleanField(default=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='move',
            name='timestamp',
            field=models.DateTimeField(default='', auto_now_add=True),
            preserve_default=True,
        ),
    ]

Solution

I had a similar thing. Delete all migrations from your folder /migrations and then run python manage.py makemigrations and then python manage.py migrate. This worked for me.



Answered By - Benjamin
Answer Checked By - Senaida (Python.Easybugfix Volunteer)

Related Posts

Post a Comment