我正在用Django写一个简单的游戏,所有的事情都做对了,但是突然…,我遇到了以下错误:
我不知道这些代码有什么问题:
(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
我给了 auto_now_add=True,
auto_now_add=True
但是起初,当我跑步时:
python manage.py makemigrations
它要求我输入一个默认值 DateTimeField()
DateTimeField()
我该怎么办?
# -*- 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, ), ]
我有类似的事情。从文件夹/ migrations删除所有迁移,然后运行python manage.py makemigrations,然后运行python manage.py migration。这对我有用。