您正在使用sqlite3数据库处理django python应用程序。我对我在models.py中定义的django用户模型的扩展如下:
#Account Model
class Account(models.Model):
user = models.OneToOneField(User)
avatar_url = models.CharField(max_length=200)
profile_url = models.CharField(max_length=200)
account_type = models.CharField(max_length=60, choices=choices.ACCOUNT_TYPE)
我还有一种创建Account object
和post_save
处理程序的方法,如下所示:
#Function to Create user Account/Profile
def create_user_account(sender, instance, created, **kwargs):
if created:
models.Account.objects.create(user=instance)
#Create User / User Registration
def UserRegistration(request):
if request.method == 'POST':
username = request.POST['fn'].capitalize() + ' ' + request.POST['ln'].capitalize()
# CREATE USER
newuser = User.objects.create_user(username=username,
email=request.POST['email'],
password=request.POST['pw'])
newuser.save()
return HttpResponse(username)
#Post Save handler to create user Account/Profile
post_save.connect(create_user_account, sender=User)
现在,每当我尝试注册新用户时,我都会收到以下数据库错误:
DatabaseError at /register/
table engine_account has no column named user_id
Request Method: POST
Request URL: http://localhost:8000/register/
Django Version: 1.4
Exception Type: DatabaseError
Exception Value:
table engine_account has no column named user_id
Exception Location: /usr/local/lib/python2.7/dist-packages/Django-1.4-py2.7.egg/django/db/backends/sqlite3/base.py in execute, line 337
Python Executable: /usr/bin/python
Python Version: 2.7.3
我不知道"user_id"
字段来自哪里......任何想法?
PS:
table engine_account基本上是名为Account
的应用程序中的Engine
类
答案 0 :(得分:7)
您在运行syncdb后编辑models.py吗?
如果是这样,那么您应该使用以下方法手动编辑表格或重新创建数据库:
python manage.py syncdb
在项目目录中应用更改。
答案 1 :(得分:4)
syncdb
。因此,对于django 1.9或更高版本,运行python manage.py makemigrations <app-name>
然后python manage.py migrate
答案 2 :(得分:1)
我尝试了 python manage.py syncdb
,这还不够,所以解决方案是:
我删除了 migrations file
和
我删除了 db.sqlite3
个文件,
我执行了 python manage.py makemigrations
和 python manage.py migrate
宾果游戏!