[英]Django 1.7 migrations - datetime typeerror
I have a big issue here, I just cannot migrate my database as I want. 我这里有个大问题,我无法按需迁移数据库。 I test a lot of things (even write directly in database...). 我测试了很多东西(甚至直接在数据库中写...)。 It runs with Django 1.7.1 and my database is store with Sqlite3. 它与Django 1.7.1一起运行,而我的数据库与Sqlite3存储在一起。 So here's my issue: 所以这是我的问题:
I have a model like this: 我有一个这样的模型:
from django.db import models
import datetime
class UserUsingTime(models.Model):
userid = models.CharField(max_length = 30)
using_time = models.DateTimeField(default = datetime.time())
(The thing that I want to do is to store a timer. Like maybe all times in a race for example.) (我想做的就是存储一个计时器。例如,像比赛中的所有时间一样。)
And I add this line at the end of my model: 然后在模型的末尾添加以下行:
app_name = models.CharField(max_length = 20, null = True)
Then I run python manage makemigrations my_app_name
and it results: 然后我运行python manage makemigrations my_app_name
,结果是:
Migrations for 'my_app_name':
0003_userusingtime_app_name.py:
- Add field app_name to userusingtime
So for now all is good :) But when I try to run python manage migrate
then I have tons of errors lines stuff: 所以现在一切都很好:)但是,当我尝试运行python manage migrate
我发现了很多错误行:
Operations to perform:
Apply all migrations: admin, contenttypes, my_app_name, auth, sessions
Running migrations:
Applying my_app_name.0003_userusingtime_app_name...Traceback (most recent call last):
File "./manage.py", line 8, in
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 167, in add_field
self._remake_table(model, create_fields=[field])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 128, in _remake_table
self.create_model(temp_model)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 209, in create_model
definition, extra_params = self.column_sql(model, field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 121, in column_sql
default_value = self.effective_default(field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 184, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 627, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1286, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1269, in get_prep_value
value = super(DateTimeField, self).get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1171, in get_prep_value
return self.to_python(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1228, in to_python
parsed = parse_datetime(value)
File "/usr/local/lib/python2.7/dist-packages/django/utils/dateparse.py", line 70, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or buffer
So I really don't know what to do next... Plus it seems there is a datetime
errors and I can't explain that because I did not touch any datetime
object in my models... So weird! 所以我真的不知道下一步该怎么做...而且似乎有一个datetime
错误,我无法解释,因为我没有触摸模型中的任何datetime
对象...太奇怪了!
Please help me out with this issue :) I thank you in advance guys (Y) ! 请帮我解决这个问题:)谢谢你们(男)!
Your using_time
field definition is faulty. 您的using_time
字段定义错误。 Django is complaining because you're trying to store a time
object in a datetime
container. Django抱怨是因为您试图将time
对象存储在datetime
容器中。
DateTimeField
is simply the wrong kind of field for what you're trying to represent (a length of time). 对于您要表示的内容(时间长度), DateTimeField
只是错误的字段类型。 You could switch this to a TimeField
, or simply use a FloatField
or IntegerField
to store the seconds or milliseconds. 您可以将其切换为TimeField
,或仅使用FloatField
或IntegerField
来存储秒或毫秒。
(Why is this only throwing a migrations error now? I assume it's because this bug wasn't caught in an earlier version of Django, and this is the first real migration (ie not the initial, fake migration) that you've tried in 1.7.) (为什么现在只抛出迁移错误?我想是因为这个错误没有被早期版本的Django捕获,这是您尝试过的第一个真正的迁移(即,不是初始的,伪造的迁移)。 1.7。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.