I am writing a simple game in Django, all of things were right, but suddenly..., I was encountered by following error:
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."]
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?
# -*- 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,
),
]
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.
As far as I understand, you already have a database which already has some "Move" entries in it.
If you add a column in a table which already has data in it, you'll need to provide a default value for that column, that the migration will set to all existing entries in the DB for the involved table (otherwise such entries will be invalid, unless null=True is specified as kwarg, if I remember correctly)
Furthermore, it is possible (happens to me ALL THE TIME), that you will need to set, in settings.py, the DATE_INPUT_FORMATS and the DATETIME_INPUT_FORMATS variables, accordingly to your locale and the way you're used to type dates. (See https://docs.djangoproject.com/en/1.7/ref/settings/#date-input-formats )
An example (In Italy, we have DD/MM/YYYY format):
DATE_INPUT_FORMATS = ( "%d/%m/%Y", )
DATETIME_INPUT_FORMATS = ( "%d/%m/%Y %H:%M", )
You django configuration is expecting the following format instead:
YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]
(Stuff inside the square brackets is optional)
Edit: the auto_now_add kw arg tells that the field value should be set to "now" when adding (and not updating..) an entry
It asked me for entering a default value for DateTimeField()
It is doing this because you have not specified a default, and the field is not optional. Since its a destructive change, you have to provide a default value.
You probably just hit enter and hence the exception, since a blank string is not a valid entry for DateTimeField
.
The solution is to run migrate again, and this time provide a valid date and time string; which will be used for all existing rows in your database; example of a valid format is 2015-02-12 00:00
Also, if you're not afraid of getting your hands a little dirty, looking through the migrations files in that folder and finding the specific invalid field did it for me. Deleting all the migrations threw up a few errors for me.
Solution (if you are using Visual Studio Code and you don't want to delete all files from the migrations
folder):
Press ctr+shift+f
type the phrase that was seen as invalid.
For example:
django.core.exceptions.ValidationError: ['“09.09.2022” value has an invalid date format. It must be in YYYY-MM-DD format.']
so type 09.09.2022
Be sure that you are looking at all files!
Below the 'search' field, place in the 'replace' field a proper date: 2022.09.09
voila
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.