简体   繁体   English

在django-allauth中的用户模型中添加列的正确方法是什么?

[英]What is the proper way to add column in user model in django-allauth?

Below is what i have tried to add a phone number column in user model:- 以下是我尝试在用户模型中添加电话号码栏的内容:-

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'

But on this change, it raises the OperationalError: (1054, "Unknown column 'users_user.phonenumber' in 'field list'") 但是,在进行此更改时,它会引发OperationalError:(1054,““字段列表”中的未知列'users_user.phonenumber'”)

I already uses syncdb and migrate options but nothing will work, As i am very new to django, please help me 我已经使用过syncdb和migration选项,但是没有任何效果,因为我对django非常陌生,请帮助我

I am using:- Python2.7, Django 1.6, django-allauth 0.15.0 我正在使用:-Python2.7,Django 1.6,django-allauth 0.15.0

Actually Problem is occuring that the field or column i created does not actually created in database and running syncdb doesn't work in this case, then finally i got the answer, we have to use South to create the schema migration to create your new table. 实际发生的问题是,在这种情况下,我创建的字段或列实际上未在数据库中创建,并且无法运行syncdb,所以最终我得到了答案,我们必须使用South创建架构迁移来创建新表。

python manage.py schemamigration appname --auto

Once we have this migration written and tested to our liking, you can run the migration and verify that it did what we expected via the Django admin. 一旦我们按照自己的喜好编写并测试了此迁移,就可以运行迁移并通过Django管理员验证其是否达到了我们的预期。

python manage.py migrate

And also made some changes in forms.py 并且还对form.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()

Try something like this: 尝试这样的事情:

# 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'

The idea is that you want to be pointing to and using your subclass, rather than the original user class. 这个想法是您要指向并使用您的子类,而不是原始用户类。 I think you'd need to make these changes in your form code as well, but just test that first (and run manage.py syncdb) to see if your new class appears with the phone number and all the other user fields. 我认为您也需要在表单代码中进行这些更改,但是只需先进行测试(然后运行manage.py syncdb),以查看新类是否与电话号码和所有其他用户字段一起出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM