DJango 인증 시스템의 전자 메일 필드를 고유하게 만들 필요가 있습니다. 이를 위해DJango 인증 시스템에서 고유 한 전자 메일 주소 만들기
내가 파이썬 3.6.3 장고 1.11.7
를 사용하고, 나는 발견이 : Making email field unique with Django User admin
을 다음 때, 나는 응용 프로그램에서 __init__.py
에 아래의 코드를 추가 인증을 나타내는 폴더. 나는 또한 그것을 프로젝트의 __init_-.py
파일에 추가하려고 시도했다.
from django.contrib.auth.models import User
User._meta.get_field("email")._unique = True
어느 쪽이든, 나는 여전히 아래에 나열된 동일한 오류가 발생합니다. 어떻게 해결합니까?
TIA
C:\WORK\Software\Python64bitv3.6\python.exe manage.py runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001F9D03BCEA0>
Traceback (most recent call last):
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\utils\autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\core\management\commands\runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\utils\autoreload.py", line 251, in raise_last_exception
six.reraise(*_exception)
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\utils\autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\apps\config.py", line 94, in create
module = import_module(entry)
File "C:\WORK\Software\Python64bitv3.6\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\WORK\AppPython\ContractorsClubBackofficeCode\authorization\__init__.py", line 1, in <module>
from django.contrib.auth.models import User
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\contrib\auth\models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\contrib\auth\base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\db\models\base.py", line 110, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\apps\registry.py", line 247, in get_containing_app_config
self.check_apps_ready()
File "C:\WORK\Software\Python64bitv3.6\lib\site-packages\django-1.11.7-py3.6.egg\django\apps\registry.py", line 125, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
업데이트
안녕 @solarissmoke 및 입력에 대한 감사합니다!
DJango에 익숙하지 않은 나는 아직도 배우고 있습니다 :-) 다음과 같이 정의되어 있습니다. DB 수준에서 수행 할 수표를 찾고 있습니다. 여기
from django import forms
from django.contrib.auth.models import User
from authorization.models import UserProfileInfo, Mstrgenstate, Mstrgencountry, Mstrgenstoretype, Mstrgensalutationtype, Mstrgenbusinesstype
class UserForm (forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta():
model = User
fields = ('username', 'email', 'password')
def clean_email(self):
# Get the email
email = self.cleaned_data.get('email')
# Check to see if any users already exist with this email as a username.
try:
match = User.objects.get(email=email)
except User.DoesNotExist:
# Unable to find a user, this is fine
return email
# A user was found with this as a username, raise an error.
raise forms.ValidationError("This email address is already in use. Please supply a different email address.")
def clean_username(self):
# Get the username
username = self.cleaned_data.get('username')
# Check to see if any users already exist with this email as a username.
try:
match = User.objects.get(username=username)
except User.DoesNotExist:
# Unable to find a user, this is fine
return username
# A user was found with this as a username, raise an error.
raise forms.ValidationError("This userid is already in use. Please supply a different userid.")
class UserProfileInfoForm (forms.ModelForm):
class Meta():
model = UserProfileInfo
fields = ('approle', 'profilepic')
forms.py는, 내가 사용하고있는 모델은 User
라고합니다. 원하는 샘플에서 MyUser
이라고 명명했습니다.
class Meta():
model = User
fields = ('username', 'email', 'password')
TO :
class Meta():
model = MyUser
fields = ('username', 'email', 'password')
오른쪽에서
: 나뿐만 아니라 아래의 변경을 무엇입니까?
TIA
감사합니다. Pls는 업데이트 부분을 참조하십시오. 이메일이 고유 한 것처럼 작동하는지 확인해야합니다. login-name을 전체 테이블의 ID로 대체하지는 않습니다. –
이 경우'USERNAME_FIELD'을 변경하지 마십시오. 여전히 모델의'email' 필드를 유일하게 설정해야합니다. 'clean' 메쏘드에서 그 이메일을 가진 기존 사용자를 확인하려고하는 것은 경합 조건 (예 : 두 명의 사용자가 동시에 등록을 시도하는 경우)에서 안전하지 않습니다. – solarissmoke
응답 해 주셔서 다시 한 번 감사드립니다. 그건 잘 알고 있습니다 :) 나는 모델 수준에서 해결책을 찾고있었습니다. 내가 사용하고있는 데이터베이스는 Oracle입니다. 이미 EMAIL 필드에 고유 한 제약 조건을 추가했습니다. 그러나, 나를 위해 - 이것이 "최후의 수단"입니다. 이 경우에는 DJango "User"모델을 사용하고 있습니다. (위 메시지의 업데이트를 참조하십시오.) - 실제 모델 (본질적으로)과 작동하도록 이메일을 설정할 필요가 없습니다. 이 상황에서 나는 무엇을 할 수 있습니까? –