【发布时间】:2014-07-19 16:15:00
【问题描述】:
以下是我尝试在用户模型中添加电话号码列的内容:-
from django.contrib.auth.models import AbstractUser
# models.py
# Import the basic Django ORM models library
from django.db import models
from django.utils.translation import ugettext_lazy as _
# Subclass AbstractUser
class User(AbstractUser):
phonenumber = models.CharField(max_length=15)
def __unicode__(self):
return self.username
# forms.py
from django import forms
from .models import User
from django.contrib.auth import get_user_model
class UserForm(forms.Form):
class Meta:
# Set this form to use the User model.
model = get_user_model
# Constrain the UserForm to just these fields.
fields = ("first_name", "last_name", "password1", "password2", "phonenumber")
def save(self, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.password1 = self.cleaned_data['password1']
user.password2 = self.cleaned_data['password2']
user.phonenumber = self.cleaned_data['phonenumber']
user.save()
# settings.py
AUTH_USER_MODEL = "users.User"
ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.UserForm'
但在此更改中,它会引发 OperationalError: (1054, "Unknown column 'users_user.phonenumber' in 'field list'")
我已经使用了 syncdb 和 migrate 选项,但没有任何效果,因为我是 django 的新手,请帮助我
我正在使用:- Python2.7、Django 1.6、django-allauth 0.15.0
【问题讨论】:
-
为什么不使用普通的
ModelForm?Meta不以常规形式做任何事情。无论如何,完整的回溯将在这里有所帮助。 -
你能不能给我提供一些很好的文档来添加这些,我没有完整的概念,我仍然需要学习这些东西,请提供文档
-
Modelforms。完整的回溯是错误,包括在遇到错误之前调用的函数的“堆栈”。您应该在错误页面的某处看到它。
标签: python django python-2.7 django-allauth