V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
daodao
V2EX  ›  Python

django1.5 自定义User模块后执行 syncdb 总是报错 TypeError: create_superuser() takes exactly 4 arguments (3 given)

  •  
  •   daodao · 2014-01-31 23:18:45 +08:00 · 3958 次点击
    这是一个创建于 4046 天前的主题,其中的信息可能已经有所发展或是发生改变。
    django1.5 自定义User模块后执行 syncdb 总是报错 ```TypeError: create_superuser() takes exactly 4 arguments (3 given)```


    http://sfault-image.b0.upaiyun.com/92/6f/926fa441e09e077c397cea5d8fad422d

    源码如下:

    # coding: utf8
    from django.db import models
    from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
    # Create your models here.
    class PassportManager(BaseUserManager):
    def create_user(self, email, alias, password=None):
    # create_user()参数必须是USERNAME_FIELD 和 所有的REQUIRED_FIELD
    if not email:
    raise ValueError('用户必须输入Email地址')
    user = self.model(
    email = PassportManager.normalize_email(email),
    alias = alias,
    # is_staff = False,
    # is_active = True,
    # is_superuser = False
    )
    user.set_password(password)
    user.save(using = self._db)
    return user
    def create_superuser(self, email, alias, password):
    # 创建超级用户,一般来说可以直接调用create_user方法,然后设置管理员相关属性,
    user = self.create_user(
    email = email,
    alias = alias,
    password = password
    )
    user.is_admin = True
    # is_admin = True 等同于以下:
    # user.is_staff = True
    # user.is_active = True
    # user.is_superuser = True
    user.save(using = self._db)
    return user
    class Passport(AbstractBaseUser):
    email = models.EmailField(max_length=254, unique=True, db_index=True)
    alias = models.CharField(max_length=60)
    is_active = models.BooleanField('active', default=True)
    is_admin = models.BooleanField('staff status', default=False, help_text='flag for log into admin site.')
    avatar = models.ImageField(upload_to='avatar', max_length=255)
    bio = models.CharField(max_length=255)
    GENRE_CHOICES = (
    (1, '摄影师'),
    (2, '插画师'),
    (3, '原画师'),
    (4, '平面设计师'),
    (5, '品牌顾问'),
    (6, '其他'),
    )
    genre = models.SmallIntegerField(choices=GENRE_CHOICES)
    site_dribbble = models.URLField(blank=True, verbose_name=u'Dribbble')
    site_deviantart = models.URLField(blank=True, verbose_name=u'DeviantART')
    site_benhance = models.URLField(blank=True, verbose_name=u'Benhance')
    site_tumblr = models.URLField(blank=True, verbose_name=u'Tumblr')
    site_zcool = models.URLField(blank=True, verbose_name=u'站酷')
    site_blog = models.URLField(blank=True, verbose_name=u'博客')
    site_sina = models.URLField(blank=True, verbose_name=u'新浪微博')
    # 用于登录的字段
    USERNAME_FIELD = 'email'
    # REQUIRED_FIELDS must contain all required fields on your User model,
    # but should not contain the USERNAME_FIELD or password as these fields
    # will always be prompted for.
    REQUIRED_FIELD = ['alias']
    objects = PassportManager()
    def get_full_name(self):
    return self.alias
    def get_short_name(self):
    return self.alias
    def __unicode__(self):
    return self.alias
    @property
    def is_staff(self):
    # Is the user a member of staff?
    # Simplest possible answer: All admins are staff
    return self.is_admin
    view raw gistfile1.py hosted with ❤ by GitHub


    Google 上有一些提问,但是好像全部都是集中在 ```REQUIRED_FIELD = [''] ```的问题,然后我逐一检查了下,以我的理解应该是没有问题的了...

    http://python.6.x6.nabble.com/Syncdb-error-with-new-1-5rc1-td5001568.html
    https://groups.google.com/d/topic/django-users/RnPEUEAvvE0
    8 条回复    1970-01-01 08:00:00 +08:00
    wtracyliu
        1
    wtracyliu  
       2014-01-31 23:38:11 +08:00
    PassportManager里面那个create_superuser是不是应该用下super ??
    crazycookie
        2
    crazycookie  
       2014-01-31 23:43:07 +08:00
    现在 1.6了 请更新
    ps:是第一次syncdb? 数据库是空的么?
    python manage.py create superuser 貌似这个可以,具体请:
    python manage.py help
    alexapollo
        3
    alexapollo  
       2014-01-31 23:44:06 +08:00
    DJANGO 1.5:def create_superuser(self, username, email, password, **extra_fields):
    你的:def create_superuser(self, email, alias, password):

    按着上面那种试试?
    daodao
        4
    daodao  
    OP
       2014-01-31 23:54:19 +08:00
    @crazycookie 数据库里面没有内容,但是已经创建了zhuo这个数据库
    daodao
        5
    daodao  
    OP
       2014-01-31 23:54:36 +08:00
    搞不定啊...一晚上了...
    ritksm
        6
    ritksm  
       2014-01-31 23:55:45 +08:00
    哈哈哈。。。LS的同学们能靠谱点么。。。根据源代码。。。于是LZ少了个'S'

    daodao
        7
    daodao  
    OP
       2014-01-31 23:58:24 +08:00
    @ritksm ...大侠NB...又是这种低级错误!
    RIcter
        8
    RIcter  
       2014-02-01 09:32:22 +08:00
    最近也在写一个django的论坛,各种重写..各种翻django源码....
    虽然django方便不少,但是有时候还是局限的要死(●´ω`●)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1231 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 17:48 · PVG 01:48 · LAX 09:48 · JFK 12:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.