2013-05-06 44 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