2016-01-22 47 views
0

我试图在用户可以放入其“年龄”,“城市”和“状态”的应用程序中添加输入。它已经适用于“first_name”,“last_name”和“email”,但是当我添加这些字段时,我得到了这个错误。Django:“state”是此函数的无效关键字参数

角色模型:

class Person(models.Model): 
    """ The model which defines a user of the application. Contains important 
    information like name, email, city/state, age, etc """ 
    user = models.OneToOneField(User) 
    first_name = models.CharField(max_length=200, null=True) 
    last_name = models.CharField(max_length=200, null=True) 
    email = models.CharField(max_length=200, null=True) 
    city = models.CharField(max_length=200, null=True) 
    state = models.CharField(max_length=200, null=True) 
    age = models.CharField(max_length=50, null=True) 

查看创建帐户:

def create_account(request): 
    # function to allow a user to create their own account 
    if request.method == 'POST': 
     # sets a new user and gives them a username 
     new_user = User(username = request.POST["username"], 
        email=request.POST["email"], 
        first_name=request.POST["first_name"], 
        last_name=request.POST["last_name"], 
        age=request.POST["age"], 
        city=request.POST["city"], 
        state=request.POST["state"]) 
     # sets an encrypted password 
     new_user.set_password(request.POST["password"]) 
     new_user.save() 
     # adds the new user to the database 
     Person.objects.create(user=new_user, 
          first_name=str(request.POST.get("first_name")), 
          last_name=str(request.POST.get("last_name")), 
          email=str(request.POST.get("email")), 
          age=str(request.POST.get("age")), 
          city=str(request.POST.get("city")), 
          state=str(request.POST.get("state"))) 
     new_user.is_active = True 
     new_user.save() 
     return redirect('../') 
    else: 
     return render(request, 'polls/create_account.html') 

任何想法,为什么我得到这个错误?

+1

你应该在这里使用表格。 –

回答

5

User模型中没有state字段,但您在创建new_user时试图通过它。

+0

我尝试用Person(...)替换User(...),但没有得到任何结果。我如何解决这个问题? – TyCharm

+0

对不起,我不明白你的问题。您创建了一个用户,然后将该用户分配给我可以看到的“Person”对象。如何用'Person'替换'User'?他们是2种不同的型号。 –

+0

我想一个更好的问题是,我如何扩展用户模型以具有其他属性,如“年龄”,“城市”和“国家”? – TyCharm

相关问题