我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用django.contrib.auth.models.User.USERNAME_FIELD。
def test_django_user_main_attribute(self): backend = Saml2Backend() old_username_field = User.USERNAME_FIELD User.USERNAME_FIELD = 'slug' self.assertEqual(backend.get_django_user_main_attribute(), 'slug') User.USERNAME_FIELD = old_username_field with override_settings(AUTH_USER_MODEL='auth.User'): self.assertEqual( DjangoUserModel.USERNAME_FIELD, backend.get_django_user_main_attribute()) with override_settings( AUTH_USER_MODEL='testprofiles.StandaloneUserModel'): self.assertEqual( backend.get_django_user_main_attribute(), 'username') with override_settings(SAML_DJANGO_USER_MAIN_ATTRIBUTE='foo'): self.assertEqual(backend.get_django_user_main_attribute(), 'foo')
def test_form_init(self, mock_get_user_model): mock_get_user_model.return_value = User initial_data = {'username': 'username', 'password': 'password', 'login_server': '202.141.80.10'} form = WebmailLoginForm(data=initial_data) self.assertIsInstance(form.fields['username'], forms.CharField) self.assertIsInstance(form.fields['password'], forms.CharField) self.assertIsInstance(form.fields['password'].widget, forms.PasswordInput) self.assertEqual(form.data.get('username'), initial_data.get('username')) self.assertEqual(form.data.get('password'), initial_data.get('password')) self.assertEqual(form.data.get('login_server'), initial_data.get('login_server')) self.assertEqual(form.port, poplib.POP3_SSL_PORT) self.assertEqual(form.username_field.verbose_name, User.USERNAME_FIELD)
def get_username_field(): try: username_field = User.USERNAME_FIELD except AttributeError: username_field = 'username' return username_field
def setUp(self): Group.objects.create(name='group1') Group.objects.create(name='group2') Group.objects.create(name='group3') User.objects.create(**{ User.USERNAME_FIELD: "locked_user", "is_active": False })