1
请参阅我的模型和表单(Python 2.7)。运行python manage.py迁移后,只有“公司”和“区域”成功出现在数据库中。Django迁移:只有部分模型成功迁移到sql
显示错误:错误类型预期字符串或缓冲区
我曾经尝试手动添加到数据库中的列,但随后的表单数据无法保存到数据库中。
请帮助查明此迁移问题的原因。预先感谢您的帮助。
models.py
from django import forms
from django.forms import ModelForm
from django.db import models
from dupont.models import Result
from datetime import date
from django.forms import widgets
class Input(models.Model):
company=models.CharField(max_length=100)
region=models.CharField(max_length=100)
supply_chain=models.CharField(max_length=100)
uom=models.CharField(max_length=100)
start_date=models.DateField(auto_now=False, auto_now_add=False)
end_date=models.DateField(auto_now=False, auto_now_add=False)
def __unicode__(self):
return self.company
forms.py
from django import forms
from django.forms import ModelForm
from .models import Input
from dupont.models import Result
from django.contrib.auth.models import User,Group
from datetimewidget.widgets import DateTimeWidget, DateWidget, TimeWidget
from django.forms import widgets
from functools import partial
from django.forms.util import ErrorList
DateInput = partial(forms.DateInput, {'class': 'datepicker'})
class InputForm(forms.ModelForm):
company=forms.CharField(widget=forms.TextInput, label="Company",error_messages={'required': 'Please enter the company name'},required=True)
regionlist = forms.ModelChoiceField(queryset=Result.objects.values('region').distinct())
sc_list=forms.ModelChoiceField(queryset=Result.objects.values('supply_chain').distinct())
uom=forms.CharField(required=True)
start_date=forms.DateField(widget=DateInput())
end_date = forms.DateField(widget=DateInput())
error_css_class='error'
required_css_class = 'required'
class Meta:
model = Input
fields = ('company', 'region','supply_chain','uom','start_date','end_date')
widgets = {
'start_date': forms.DateInput(attrs={'class':'datepicker'}),
'end_date': forms.DateInput(attrs={'class':'datepicker'}),
}
错误
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\managem
ent\__init__.py", line 338, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\managem
ent\__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\managem
ent\base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\managem
ent\base.py", line 444, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\managem
ent\commands\migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\migration
s\executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=f
ake_initial)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\migration
s\executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\migration
s\migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\migration
s\operations\fields.py", line 62, in database_forwards
field,
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\backends\
mysql\schema.py", line 43, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\backends\
base\schema.py", line 384, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\backends\
base\schema.py", line 146, in column_sql
default_value = self.effective_default(field)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\backends\
base\schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\models\fi
elds\__init__.py", line 710, in get_db_prep_save
prepared=False)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\models\fi
elds\__init__.py", line 1322, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\models\fi
elds\__init__.py", line 1317, in get_prep_value
return self.to_python(value)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\models\fi
elds\__init__.py", line 1274, in to_python
parsed = parse_date(value)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\utils\datepa
rse.py", line 60, in parse_date
match = date_re.match(value)
TypeError: expected string or buffer
谢谢。我删除应用程序中的模式和迁移文件。同时删除日期栏中的“auto_now = False,auto_now_add = False”。然后重新运行syncdb代码。然后它现在起作用。 –