我将模型迁移到数据库时遇到问题。
models.py:
# coding: utf-8
from django.db import models
# Create your models here.
class Product(models.Model):
title = models.CharField('title', max_length=100, blank=False)
author = models.CharField('author', max_length=100, blank=False)
def __unicode__(self):
return "%s - %s" % (self.title,self.author)
class Meta:
verbose_name = 'Produkt'
verbose_name_plural = 'Produkty'
这是错误日志:
Operations to perform:
Synchronize unmigrated apps: django_extensions
Apply all migrations: admin, TestApp, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying TestApp.0001_initial... OK
Applying TestApp.0002_auto_20141104_1305... OK
Applying TestApp.0003_auto_20141104_1312... OK
Applying TestApp.0004_genretype_producttype... OK
Applying TestApp.0005_auto_20141104_1322... OK
Applying TestApp.0006_auto_20141104_1338... OK
Applying TestApp.0007_product_producttype... OK
Applying TestApp.0008_auto_20141106_0852...Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 3.4.1\helpers\pycharm\django_manage.py", line 23, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python27\lib\runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\Martin\PycharmProjects\SPJA_PROJECT_Videoshop\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 338, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 139, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 473, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 190, in _alter_field
self._remake_table(model, alter_fields=[(old_field, new_field)])
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 128, in _remake_table
self.create_model(temp_model)
File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 209, in create_model
definition, extra_params = self.column_sql(model, field)
File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 121, in column_sql
default_value = self.effective_default(field)
File "C:\Python27\lib\site-packages\django\db\backends\schema.py", line 184, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 627, in get_db_prep_save
prepared=False)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1176, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1171, in get_prep_value
return self.to_python(value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1128, in to_python
parsed = parse_date(value)
File "C:\Python27\lib\site-packages\django\utils\dateparse.py", line 37, in parse_date
match = date_re.match(value)
TypeError: expected string or buffer
Process finished with exit code 1
我是Django的初学者并且没有太多经验。谁能说我,我做错了什么?错误在哪里?我试着找了最近两个小时而没有。
以下是无效的迁移:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('TestApp', '0007_product_producttype'),
]
operations = [
migrations.RemoveField(
model_name='product',
name='author',
),
migrations.AddField(
model_name='product',
name='director',
field=models.CharField(default=b'', max_length=100, verbose_name=b'director'),
preserve_default=True,
),
migrations.AlterField(
model_name='product',
name='genre',
field=models.OneToOneField(default=b'', to='TestApp.GenreType'),
preserve_default=True,
),
migrations.AlterField(
model_name='product',
name='mediatype',
field=models.OneToOneField(default=b'', to='TestApp.MediaType'),
preserve_default=True,
),
migrations.AlterField(
model_name='product',
name='year',
field=models.DateField(default=2000, verbose_name=b'year'),
preserve_default=True,
),
]
答案 0 :(得分:0)
0008_auto_20141106_0852
中的迁移无效:
migrations.AlterField(
model_name='product',
name='year',
field=models.DateField(default=2000, verbose_name=b'year'),
preserve_default=True,
),
您有DateField
,但默认值是整数。您需要将此字段从DateField
更改为IntegerField
,然后再次运行迁移:
migrations.AlterField(
model_name='product',
name='year',
field=models.IntegerField(default=2000, verbose_name=b'year'),
preserve_default=False,
),
进行此更改,您的迁移应该可以正常运行。