我的UserProfile工作正常,直到我更改了模型和表單。我在模型中添加了一個DateField並更新了我的Forms.py和模板。還做了一個syncdb。在Django模型中添加新字段後出現錯誤
資料/ 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'")
如果我手動創建,是否有任何預防措施?或者我只是去使用mysql工作臺手動添加表,這就是它? – John 2012-04-12 17:53:30
@John理想情況下,您使用南方的應用程序。但是,對於只有一列,它可能很麻煩。在你的特定'app'上執行[sqlall](https://docs.djangoproject.com/en/dev/ref/django-admin/#sqlall-appname-appname)可以準確地告訴你Django本身會用到什麼DDL語法創造它。 – 2012-04-12 17:57:12