2012-04-12 16 views
2

モデルとフォームを変更するまで、私の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)) 

これは、スローされているエラーです。あなたはそれに新しい列を追加しませんsyncdb新しい列を追加する前にテーブルが存在していた場合

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'") 

答えて

2

syncdbdoes not alter existing tablesDjango southを使用するか、列を手動で追加することを検討してください。

+0

手動で作成している場合は、予防措置はありますか?または、私はちょうど行くとmysqlのワークベンチとthatsを使用して手動でテーブルを追加する? – John

+0

@John理想的にはあなたは南のアプリを使用します。しかし、1列の場合は面倒です。あなたの特定の 'app'の[sqlall](https://docs.djangoproject.com/en/dev/ref/django-admin/#sqlall-appname-appname)を実行すると、Django自体がどんなDDL構文それを作成します。 –

1

Syncdbは自動的に新しいフィールドを作成しません。スキーマ変更をDBに適用するには、テーブルを完全に削除してsyncdbを実行する必要があります。

ほとんどのdjango開発者は、これらのタイプの移行を処理するために、southというサードパーティアプリケーションを使用しています。 Southを使用すると、フィールドを追加して、データベースを再作成しなくても、データベースを手動で変更しなくても、データベースを移行できます。あなたが./manage.py syncdbの実行を実行すると

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

は、Djangoは定義されているすべての新しいモデルを探し、それらの新しいモデルを表現するために、データベースのテーブルを追加します。しかし、既存のモデルを変更すると、./manage.py syncdbはデータベースを変更しません。

Django EvolutionはDjangoの拡張版で、時間の経過とともにモデルの変更を追跡し、その変更を反映するようにデータベースを更新することができます。

関連する問題