django - 在 Django 模型中添加新字段后出现错误

标签 django

在我更改模型和表单之前,我的 UserProfile 工作正常。我在模型中添加了 DateField 并更新了 Forms.py 和模板。还做了一个syncdb。

配置文件/models.py

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

个人资料/forms.py

class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfiles
        fields = ('some_field', 'birthday', 'otherfields')

个人资料/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 将不会向其中添加新列! syncdb does not alter existing tables 。考虑使用Django south或者手动添加该列。

关于django - 在 Django 模型中添加新字段后出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10128620/

相关文章:

python - 如何在 django 的 forms.py 文件中使用 django-colorfield ?

iphone - Web 应用程序 GPS 轮询

python - 如何在没有 HTTPS 的情况下运行 Django 1.6 LiveServerTestCase?

django - 显示 Django 消息框架消息

django - Azure 上 Django 的 GitHub : Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

python - 如何更改 Django 中 MinValueValidator 错误消息中的日期格式?

javascript - 使用 Mapbox-gl-js 和 Django 的不同 map 图钉

python - django 编写我的第一个自定义模板标签和过滤器

python - django - urls.py 根路径集

django - 如何在 Django 中使用 manage.py 列出所有已安装的应用程序?