【问题标题】:What is the proper way to add column in user model in django-allauth?在 django-allauth 的用户模型中添加列的正确方法是什么?
【发布时间】: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

【问题讨论】:

  • 为什么不使用普通的ModelFormMeta 不以常规形式做任何事情。无论如何,完整的回溯将在这里有所帮助。
  • 你能不能给我提供一些很好的文档来添加这些,我没有完整的概念,我仍然需要学习这些东西,请提供文档
  • Modelforms。完整的回溯是错误,包括在遇到错误之前调用的函数的“堆栈”。您应该在错误页面的某处看到它。

标签: python django python-2.7 django-allauth


【解决方案1】:

实际上问题是我创建的字段或列实际上并没有在数据库中创建并且在这种情况下运行syncdb不起作用,最后我得到了答案,我们必须使用South来创建架构迁移创建新表。

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()

【讨论】:

    【解决方案2】:

    试试这样的:

    # 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)以查看您的新类是否与电话号码和所有其他用户字段一起出现。

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2019-09-13
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多