本文介绍了& quot; django.core.exceptions.ValidationError& quot;错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Django写一个简单的游戏,所有的事情都是正确的,但是突然...,我遇到了以下错误:
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."]
在我的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
我给了 auto_now_add = True
,
但是首先,当我运行时:
but at first, when I run :
python manage.py makemigrations
它要求我输入 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,
),
]
推荐答案
我有类似的事情.从文件夹/migrations删除所有迁移,然后运行python manage.py makemigrations,然后运行python manage.py migration.这对我有用.
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.
这篇关于& quot; django.core.exceptions.ValidationError& quot;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!