- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个 fixture 来从自定义用户模型生成 Django 用户。奇怪的是,新创建的用户结果默认经过身份验证,而我希望它是匿名的。我做错了什么?
fixture :
import pytest
@pytest.fixture
def create_user(db, django_user_model):
def make_user(**kwargs):
return django_user_model.objects.create_user(**kwargs)
return make_user
测试 fixture :
@pytest.mark.django_db
def test_fixture_create_user(create_user):
user = create_user(email='foo@bar.com', password='bar')
assert user.is_authenticated is True # <-- this should be False but it's True
assert user.is_anonymous is True # <-- this fails
测试以这种方式失败:
E assert False is True
E + where False = <CustomUser: foo@bar.com>.is_anonymous
自定义用户模型:
class CustomUser(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
自定义用户管理器:
class CustomUserManager(BaseUserManager):
"""
Custom user model manager where email is the unique identifiers
for authentication instead of usernames.
"""
def create_user(self, email, password, **extra_fields):
"""
Create and save a User with the given email and password.
"""
if not email:
raise ValueError(_('The Email must be set'))
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
最佳答案
没有什么不对,伙计。 AbstractUser
的 is_authenticated
属性 is hardcoded to be True
. Django 的中间件正在发挥所有的作用,并返回 request.user
作为 AnonymousUser
类实例,其中 is_authenticated
属性返回 False
关于django - pytest-django:使用 create_user 创建的用户始终经过身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61595777/
我想捕获它们并向用户返回有用的消息。文档 https://docs.djangoproject.com/en/1.4/topics/auth/表示在给定无效用户名时会引发 ValueError。当用户
我正在尝试创建自定义配置文件以将其他字段添加到 django-registration。这是我到目前为止的代码 -- 在models.py中 from django.db import models
我遇到过create()的一些可疑行为User的方法对象管理器。看起来像 password创建 User 不需要字段如果您使用此方法,则对象。结果你得到 User空白 password .如果您使用
我正在关注 django's doc关于创建自定义用户模型,我在定义 create_user() 和 create_superuser() 时感到困惑。定义 create_user() 时,传递的密码
我正在尝试编写一个 fixture 来从自定义用户模型生成 Django 用户。奇怪的是,新创建的用户结果默认经过身份验证,而我希望它是匿名的。我做错了什么? fixture : import pyt
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: Django Password Generator d = Data.objects.get(key=key)
我删除了 username字段,因为我希望用户能够使用他们的电子邮件地址登录,所以我在 models.py 中有这个: class CustomUser(AbstractUser): USER
from django.contrib.auth.models import User a = User.objects.create_user("1","2","3") b = User.objec
我遵循了这个自定义 Django 用户模型中的代码 example几乎一字不差,当我尝试创建一个用户时,它在 self.model 的最后一个参数上失败并出现错误 TypeError: 'NoneTy
我是 Python 和 Django 中单元测试和模拟的新手。 这是我的模型(简化版): from django.db import models from django.contrib.auth.m
我刚开始使用 django-cms,但遇到了一些问题。我用的是virtualenv,请大家帮忙,错误如下: Traceback (most recent call last): File "crea
测试失败: class LoginTest(TestCase): def test_login(self): self.user = User(username='test',
我自定义了 Django 的身份验证,现在每次我尝试使用 Django Admin 创建新用户时,都会直接保存该用户的密码,而无需进行哈希处理。我发现没有调用 BaseUserManager 类的 c
我正在尝试完成 Django 自定义用户管理器的教程。它们中的大多数都像文档中所说的那样覆盖 create_user() 和 create_super_user(),但是本教程将这两个方法单独保留,而
我没有使用 django 的 auth 模块,而是使用了自己的模块,并且已经非常后悔了。 为了纠正这种情况,我正在尝试将数据从我的 User 模型迁移到 django.auth.models.User
您好,我在 Django 1.7 中获取用户的用户名和密码,但出现错误 - “Manager”对象没有属性“create_user” 我的观点.py def register(request):
模型.py: from django.contrib.auth.models import AbstractBaseUser from django.contrib.auth.mode
我有一个使用 mongoengine 和 flask-security 的 python flask 应用程序,这些应用程序是根据示例构建的,以公开所有确认、注册、跟踪和恢复功能。 一切正常,除了用户
假设我想注册一个用户(我正在使用位于 django.contrib.auth.models 中的用户模型)。假设这是我的 serializers.py: class UserSerializer(se
假设我想注册一个用户(我正在使用位于 django.contrib.auth.models 中的用户模型)。假设这是我的 serializers.py: class UserSerializer(se
我是一名优秀的程序员,十分优秀!