小编7tu*_*pel的帖子

Django自定义用户模型和usermanager

我正在使用Django 1.5构建一个Web应用程序.我正在使用自定义用户模型和自定义UserManager.我按照官方Django文档的说明和示例进行操作.

现在,当我试图通过UserManager.create_user(...) 我收到一个NoneType错误来创建一个新用户时 :似乎UserManager的属性模型是None类型.我想我正在User模型中正确设置UserManager( objects = UserManager() )

我真的不知道我在哪里弄错了.展示我的编码伙伴,我是Django的新手.也许你可以帮助我们.

这是代码:

class UserManager(BaseUserManager):
"""
    create a new user

    @param username:  the name for the new user
    @param password:  the password for the new user. if none is provided a random password is generated
    @param person:    the corresponding person object for this user
"""
def create_user(self, username, person, password=None):
    if not username:
        raise ValueError('User must have a valid username')

    user = self.model(username=username, created=datetime.now(), must_change_password=True, deleted=False, person=person)

    user.set_password(password)
    user.save(using=self._db)
    return user …
Run Code Online (Sandbox Code Playgroud)

python django django-models django-users

6
推荐指数
2
解决办法
1万
查看次数

Django查询速度极慢

我遇到了Django应用程序的问题.对模型Scope的查询极其缓慢,经过一些调试后我仍然不知道问题所在.

当我scope = Scope.objects.get(pk='Esoterik I')在django中查询db 时需要5到10秒.该数据库的主键少于10个条目和索引.所以这太慢了.在db上执行等效查询时,SELECT * FROM scope WHERE title='Esoterik I';一切正常,只需要大约50ms.

如果我使用一组结果进行查询,scope_list = Scope.objects.filter(members=some_user)然后执行例如print(scope_list)或迭代列表的元素,则会出现同样的问题.查询本身只需要几毫秒,但元素的打印或迭代再次需要5到10秒,但该集只有两个条目.

数据库后端是Postgresql.问题在本地开发服务器和apache上发生相同.

这里是模型的代码:

class Scope(models.Model):
    title = models.CharField(primary_key=True, max_length=30)

    ## the semester the scope is linked with
    assoc_semester  =   models.ForeignKey(Semester, null=True) 

    ## the grade of the scope. can be Null if the scope is not a class
    assoc_grade     =   models.ForeignKey(Grade, null=True)

    ## the timetable of the scope. can be null if the scope is not direct associated with …
Run Code Online (Sandbox Code Playgroud)

database django performance django-queryset django-postgresql

5
推荐指数
1
解决办法
7590
查看次数