Custom Form Validation in Django-Allauth

萝らか妹 提交于 2019-12-09 03:13:31

问题


I want to do some extra validation on fields in django-allauth. For example I want to prevent using free email addresses. So I want to run this method on signup

def clean_email(self):
    email_domain = self.cleaned_data['email'].split('@')[1]
    if email_domain in self.bad_domains:
        raise forms.ValidationError(_("Registration using free email addresses is prohibited. Please supply a different email address."))

Similarly I want to run custom validation on different fields other than email address. How can I perform this?


回答1:


There are some adapters on the allauth configuration. For example this one:

ACCOUNT_ADAPTER (="allauth.account.adapter.DefaultAccountAdapter")
    Specifies the adapter class to use, allowing you to alter certain default behaviour.

You can specify a new adapter by overriding the default one. Just override the clean_email method.

class MyCoolAdapter(DefaultAccountAdapter):

    def clean_email(self, email):
        """
        Validates an email value. You can hook into this if you want to
        (dynamically) restrict what email addresses can be chosen.
        """
        *** here goes your code ***
        return email

Then modify the ACCOUNT_ADAPTER on the settings.py

ACCOUNT_ADAPTER = '**app**.MyCoolAdapter'

Check the default behavior on: https://github.com/pennersr/django-allauth/blob/master/allauth/account/adapter.py



来源:https://stackoverflow.com/questions/23304588/custom-form-validation-in-django-allauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!