2013-05-06 40 views
2

如何覆蓋allauth.account.forms.BaseSignupForm中的默認clean_email()方法。 我試着在Forms.py如下:覆蓋Django-allauth中的clean_email()方法

from allauth.account.forms import BaseSignupForm 

    class Extended_BaseSignupForm(BaseSignupForm): 
     def clean_email(self): 
      data = self.cleaned_data['email'] 
      if "@gmail.com" not in data: # any check you need 
       raise forms.ValidationError("Must be a gmail address") 
      if app_settings.UNIQUE_EMAIL: 
       if data and email_address_exists(data): 
        raise forms.ValidationError \ 
         (_("A user is registered with this e-mail address.")) 
      return data 

壓倒一切的目的是爲了防止用戶一次性電子郵件ID註冊。

+0

你應該粘貼完整的urls.py。 – jpic 2013-05-06 12:51:59

回答

5

在即將推出的allauth版本中,這已經變得更容易。你可以簡單地覆蓋clean_email適配器方法,在這裏:

https://github.com/pennersr/django-allauth/blob/4bb9e0170f37d8196bd0c4a78e83adb7b779c113/allauth/account/adapter.py#L175

使用ACCOUNT_ADAPTER設置爲指向包含覆蓋方法您的自定義適配器。

+0

感謝您的信息,我想更新現在是唯一的解決方案。 – 2013-05-06 16:21:15

+0

@pennersr:allauth很棒。我遵循相同的邏輯,用我的正則表達式覆蓋'clean_username'。不過我想,這些修改必須在另外兩個地方複製,即'_generate_unique_username_base'(在allauth中)和'UserChangeForm'(在Django中)。那是對的嗎? – Medorator 2014-06-08 16:03:10