2016-07-27 74 views
0

我一直在研究一个涉及使用Django和AngularJS的认证页面的项目。我创建了用户类的扩展版本,并添加了“company”和“phone_number”作为字段。Django没有正确更新SQLite表

这里是我的代码的models.py:

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager 
from django.db import models 
from django.core.validators import RegexValidator 




class AccountManager(BaseUserManager): 
    def create_user(self, email, password=None, **kwargs): 
     if not email: 
      raise ValueError('Users must have a valid email address') 

     #if not kwargs.get('username'): 
      #raise ValueError('Users must have a valid username') 

     #if access_code not in ['password']: 
      #raise ValueError('Sorry you are not eligible to join') 

     account = self.model(
      email=self.normalize_email(email)) 

     account.set_password(password) 
     account.save() 

     return account 

    def create_superuser(self, email, password, **kwargs): 
     account = self.create_user(email, password, **kwargs) 

     account.is_admin = True 
     account.save() 

     return account 


class Account(AbstractBaseUser): 
    email = models.EmailField(unique=True) 

    first_name = models.CharField(max_length=40, blank=False) 
    last_name = models.CharField(max_length=40, blank=False) 
    company = models.CharField(max_length=40, blank=False) 
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.") 
    phone_number = models.IntegerField(validators=[phone_regex], blank=False, null=True) # validators should be a list 
    # access_code = models.CharField(max_length=40, blank=False, default='SOME STRING') 

    is_admin = models.BooleanField(default=False) 

    created_at = models.DateTimeField(auto_now_add=True) 
    updated_at = models.DateTimeField(auto_now=True) 

    objects = AccountManager() 

    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = ['first_name', 'last_name', 'company', 'phone_number'] 

    def __unicode__(self): 
     return self.email 

    def get_full_name(self): 
     return ' '.join([self.first_name, self.last_name]) 

    def get_short_name(self): 
     return self.first_name 

现在,当我去到终端,并执行蟒蛇manage.py createsuperuser所有字段选项弹出让我输入文字。但是,当我以后检查数据库时,只更新电子邮件和密码字段。公司,电话号码,名字和姓氏返回为''。

任何线索我做错了什么?我一直花费太多时间来解决这个问题。

感谢

+0

建议阅读:[如何创建一个最小,完整和可验证的示例](http://stackoverflow.com/help/mcve) – wogsland

回答

0

create_user,你没有通过任何其他参数的self.model调用,因此只设置了电子邮件,以及后来的密码。你也需要将kwargs传进去。

account = self.model(email=self.normalize_email(email), **kwargs)