在Django模型中添加新字段后出错

在Django模型中添加新字段后出错,django,Django,在我更改模型和表单之前,我的用户配置文件工作正常。我在模型中添加了一个日期字段,并更新了Forms.py和模板。还做了一个syncdb profile/models.py class UserProfiles(models.Model): user = models.OneToOneField(User) #other fields here birthday = models.DateField() profile/forms.py class UserProfile

在我更改模型和表单之前,我的用户配置文件工作正常。我在模型中添加了一个日期字段,并更新了Forms.py和模板。还做了一个syncdb

profile/models.py

class UserProfiles(models.Model):
    user = models.OneToOneField(User)
    #other fields here
    birthday = models.DateField()
profile/forms.py

class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfiles
        fields = ('some_field', 'birthday', 'otherfields')
profile/views.py

def editprofile(request):
    return render_to_response('profile_edit.html', {'form':UserProfileForm()}, context_instance=RequestContext(request))
这就是它抛出的错误

Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "E:\django-sample\proschools\..\proschools\profile\views.py" in generateprofile
  12.             userprofile = UserProfiles.objects.get(user=request.user)
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in get
  132.         return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in get
  344.         num = len(clone)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in __len__
  82.                 self._result_cache = list(self.iterator())
File "C:\Python27\lib\site-packages\django\db\models\query.py" in iterator
  273.         for row in compiler.results_iter():
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in results_iter
  680.         for rows in self.execute_sql(MULTI):
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  735.         cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  34.             return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py" in execute
  86.             return self.cursor.execute(query, args)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py" in execute
  174.             self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py" in defaulterrorhandler
  36.     raise errorclass, errorvalue

Exception Type: OperationalError at /profile/
Exception Value: (1054, "Unknown column 'profile_userprofiles.birthday' in 'field list'")

如果在添加新列之前该表已存在,
syncdb
将不会向其添加新列<代码>同步数据库。考虑使用或手动添加列。

SycDB不会自动为您创建新字段。您必须完全删除该表并运行syncdb才能将架构更改应用于数据库

大多数django开发人员使用一个名为的第三方应用程序来处理这些类型的迁移。South允许您添加字段并迁移数据库,而无需重新创建或手动更改数据库

http://code.google.com/p/django-evolution/
运行./manage.py syncdb时,Django将查找已定义的任何新模型,并添加一个数据库表来表示这些新模型。但是,如果对现有模型进行更改,./manage.py syncdb将不会对数据库进行任何更改

http://code.google.com/p/django-evolution/

这就是Django进化论的适用之处。Django Evolution是Django的一个扩展,它允许您跟踪模型随时间的变化,并更新数据库以反映这些变化。

如果我手动创建,有什么预防措施吗?或者我只是使用mysql workbench手动添加表,就这样?@John,理想情况下,您可以使用south应用程序。然而,对于一个列来说,它可能很麻烦。在特定的
应用程序上执行
可以准确地告诉您Django本身将使用什么DDL语法来创建它。