Python 在django allauth中,在用户模型中添加列的正确方法是什么?
以下是我尝试在用户模型中添加电话号码列的内容:-Python 在django allauth中,在用户模型中添加列的正确方法是什么?,python,django,python-2.7,django-allauth,Python,Django,Python 2.7,Django Allauth,以下是我尝试在用户模型中添加电话号码列的内容:- 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 cl
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'
但在这一更改中,它引发了操作错误:(1054,“字段列表”中的未知列“users\u user.phonenumber”)
我已经使用了syncdb和migrate选项,但没有任何效果,因为我对django非常陌生,请帮助我
我正在使用:-
Python2.7、Django 1.6、Django allauth 0.15.0尝试以下内容:
# models.py
# Subclass AbstractUser
class CustomUser(AbstractUser):
phonenumber = models.CharField(max_length=15)
def __unicode__(self):
return self.username
# settings.py
AUTH_USER_MODEL = 'myapp.CustomUser'
其思想是,您希望指向并使用您的子类,而不是原始的用户类。我认为您也需要在表单代码中进行这些更改,但只需首先测试(并运行manage.py syncdb)要查看您的新类是否与电话号码和所有其他用户字段一起出现。事实上,问题在于我创建的字段或列实际上没有在数据库中创建,并且在这种情况下运行syncdb不起作用,最后我得到了答案,我们必须使用创建模式迁移来创建新表
python manage.py schemamigration appname --auto
一旦我们按照自己的喜好编写和测试了这个迁移,您就可以运行迁移,并通过Django管理员验证它是否达到了我们预期的效果
python manage.py migrate
还对forms.py进行了一些更改
# forms.py
class UserForm(ModelForm):
class Meta:
# Set this form to use the User model.
model = User
# Constrain the UserForm to just these fields.
fields = ("username", "email", "phonenumber")
def save(self, user):
user.username = self.cleaned_data['username']
user.email = self.cleaned_data['email']
user.phonenumber = self.cleaned_data['phonenumber']
user.save()
为什么不使用常规的
ModelForm
Meta
不以常规形式执行任何操作。无论如何,完整的回溯在这里会有所帮助。你能给我提供一些很好的文档来添加这些内容吗?我还没有完整的概念,我还需要学习这些东西,请给我提供文档。完整回溯是指错误,包括在遇到错误之前调用的函数的“堆栈”。你应该在错误页面的某个地方看到它。