请查看我的模型和表格。(Python 2.7)。运行python manage.py migration后,数据库中仅成功显示“ company”和“ region”。
错误显示:类型错误预期的字符串或缓冲区
我曾经尝试手动添加数据库中的列,但是随后表单的数据无法保存到数据库。
请帮助查明此迁移问题的原因。在此先感谢您的帮助。
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
表格
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
最佳答案
也许您应该检查form's data
,看来它们不是正确的日期数据。
关于python - Django迁移:仅部分模型成功迁移到sql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33161761/