2017-12-16 8 views
0

私はDJango認証システムのEMailフィールドを一意にする必要があります。私はそうするためにはPython 3.6.3とDjango 1.11.7DJango認証システムでEMailアドレスをユニークにする

を使用しています

、私はこれを見つけた: 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レベルでチェックを行います。ここで

forms.py

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') 

、私が使用していたモデルは、Userと呼ばれています。あなたが望むサンプルでは、​​それをMyUserと名づけました。

class Meta(): 
    model = User 
    fields = ('username', 'email', 'password') 

TO:

class Meta(): 
    model = MyUser 
    fields = ('username', 'email', 'password') 

右から

:私も、以下の変更を加えることになっていますか?

TIA

答えて

1

あなたがするリンクソリューションは、任意のより多くのそれを行うための方法7歳、間違いありません。

このようなことをするより良い方法は、custom user modelを使用することです。あなたのケースでは、あなたはそれを一意にするためにemailフィールドをオーバーライドします:

class MyUser(AbstractBaseUser): 
    email = models.EmailField(unique=True) 

を電子メールは、ユーザーの一意の識別子であることを行っている場合(すなわち、それは彼らがログインするために使用するものである)、そして、あなたにもなります設定したい:

USERNAME_FIELD = 'email' 

あなたのユーザークラスに。応答のための

AUTH_USER_MODEL = 'myapp.MyUser' 
+0

感謝:

最後に、あなたの設定ファイルにこの新しいユーザーモデルを使用するようにDjangoに教えます。更新部分が表示されます。私は電子メールが一意であるかのように動作することを確認する必要があります。これは、テーブル全体のIDとしてlogin-nameを置き換えません。 –

+0

この場合、 'USERNAME_FIELD'は変更しないでください。モデルの 'email'フィールドを一意に設定する必要があります。 'clean'メソッドでその電子メールを持つ既存のユーザをチェックしようとすることは、競合状態から安全ではありません(例えば、2人のユーザが同時に登録しようとする場合)。 – solarissmoke

+0

再度お返事ありがとうございます。それは知って良いです:)私はモデルレベルで解決策を探していました。私が使用しているデータベースはOracleです。私はすでにEMAILフィールドに一意の制約を追加しました。しかし、私にとっては、これが「最後の手段」です。この場合、DJangoの "User"モデルを使用しています(上記のメッセージの更新を参照) - 実際のモデル(本来)は動作する電子メールを設定するためのものではありません。このような状況で私は何ができますか? –

関連する問題