Py学习  »  问与答

Django 创建超级用户问题,急求助

林夕_信仰 • 10 年前 • 5152 次点击  

我扩展了django的用户模型,本来用的好好的,因为需要又加入了一个外键,这个外键是一个对象,这使得我无法创建超级用户!因为在填这个外键项的时候,总是无法通过,求助啊!

这个是用户模型,纠结的外键是university_info

enter image description here

这个是create_user enter image description here

我试了很多方法来填university_info这一项,不填、乱填或者填None,但是都通不过 enter image description here

后来我用Navicat for Mysql创建了一个university_info对象,并用它的id来填超级用户,结果还是失败 enter image description here

enter image description here

最后我实在没办法了,于是在Navicat for Mysql里找到user表,手动创建了一个超级用户,不过。。。在admin后台登陆失败。。。。 enter image description here

疯了,求解!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/295
 
5152 次点击  
文章 [ 1 ]  |  最新文章 10 年前
木头lbj
Reply   •   1 楼
木头lbj    10 年前

这个。。。
你的create_superuser方法中去掉university_info这个外键字段可以啦。

def create_superuser(self, email, username, password):   
    """创建超级用户"""   
    user = self.create_user(email, password=password, username=username)  
    user.is_admin = True     
    user.save(using=self._db)   
    return user  

创建超级用户哪里需要那么多的字段呢? 抄代码得懂代码的意思恩。