2012-06-24 30 views
2

您好我正在使用sqlite3数据库的Django的python应用程序。我有一个推广到Django的在我的models.py定义用户模型如下:Django(Python):DatabaseError:x表没有列名为y

#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" FIEL d来自..任何想法?

PS:

表engine_account基本上是在指定的应用程序中的AccountEngine

+2

你忘了运行syncdb吗? –

+0

@BurhanKhalid谢谢,这是我的愚蠢,但它仍然给我一个IntegrityError“列用户名是不唯一的”..当我没有任何名称中定义的“用户名”在我的帐户模型中定义。 – Amyth

回答

6

你编辑models.py您运行执行syncdb后?

如果是这样,那么你应该手动编辑表或使用重新创建数据库:

python manage.py syncdb 

在项目目录应用更改。

+0

我看,让我检查,并感谢您的快速回复 – Amyth

+0

重新创建数据库解决了它,非常感谢朋友! – Amyth

+0

欢迎您! :) – scriptmonster

1

syncdb已弃用django 1.9及更高版本。因此,对于Django 1.9或更高版本运行python manage.py makemigrations <app-name>然后python manage.py migrate

相关问题