2015-10-16 125 views
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 

回答

1

也許你應該檢查你的form's data,看起來他們不是正確的日期數據。

+0

謝謝。我刪除應用程序中的模式和遷移文件。同時刪除日期欄中的「auto_now = False,auto_now_add = False」。然後重新運行syncdb代碼。然後它現在起作用。 –