django-allauth - 使用自定义适配器会出错:UNIQUE约束失败:main_user.email

时间:2016-04-09 07:16:36

标签: django django-allauth

我的自定义adapters.py文件包含此内容(当然,我已正确地在settings.py中链接到它):

from allauth.account.adapter import DefaultAccountAdapter


class AccountAdapter(DefaultAccountAdapter):
    def save_user(self, request, user, form, commit=True):
        data = form.cleaned_data
        user.username = data.get('username')
        user.email = data.get('email')
        user.first_name = data.get('first_name')
        user.last_name = data.get('last_name')
        user.gender = data.get('gender')
        user.birth_date = data.get('birth_date')
        user.city = data.get('city')
        user.country = data.get('country')
        if 'password1' in data:
            user.set_password(data.get('password1'))
        else:
            user.set_unusable_password()
        self.populate_username(request, user)
        if commit:
            user.save()
        return user

错误发生在allauth代码的某处,我无法弄清楚原因。我在原始适配器保存方法中也做了同样的事情,但由于某些原因它不能用于我的自定义保存方法。

如果有人想要看到它,那么这里是一个完整的堆栈跟踪:

Traceback (most recent call last):
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/allauth/account/views.py", line 177, in dispatch
    return super(SignupView, self).dispatch(request, *args, **kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/allauth/account/views.py", line 66, in dispatch
    **kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/allauth/account/views.py", line 155, in dispatch
    **kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/allauth/account/views.py", line 82, in post
    response = self.form_valid(form)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/allauth/account/views.py", line 192, in form_valid
    self.user = form.save(self.request)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/allauth/account/forms.py", line 316, in save
    self.custom_signup(request, user)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/allauth/account/forms.py", line 285, in custom_signup
    custom_form.save(user)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/contrib/auth/forms.py", line 99, in save
    user.save()
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/contrib/auth/base_user.py", line 74, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/models/base.py", line 708, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/models/base.py", line 736, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/models/base.py", line 820, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/models/base.py", line 859, in _do_insert
    using=using, raw=raw)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/models/query.py", line 1039, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
    cursor.execute(sql, params)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/mikisoft/django-dev/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: main_user.email

1 个答案:

答案 0 :(得分:0)

我已修复它,问题在于我的注册表单(在forms.py中)。它现在看起来像这样:

from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.forms import extras
from django.contrib.auth import get_user_model

BIRTH_DATE_YEARS = ('2015','2014','2013','2012','2011','2010','2009','2008','2007','2006','2005','2004','2003','2002',
                    '2001','2000','1999','1998','1997','1996','1995','1994','1993','1992','1991','1990','1989','1988',
                    '1987','1986','1985','1984','1983','1982','1981','1980','1979','1978','1977','1976','1975','1974',
                    '1973','1972','1971','1970','1969','1968','1967','1966','1965','1964','1963','1962','1961','1960',
                    '1959','1958','1957','1956','1955','1954','1953','1952','1951','1950','1949','1948','1947','1946',
                    '1945','1944','1943','1942','1941','1940','1939','1938','1937','1936','1935','1934','1933','1932',
                    '1931','1930','1929','1928','1927')


class RegistrationForm(UserCreationForm):
    birth_date = forms.DateField(widget=extras.SelectDateWidget(years=BIRTH_DATE_YEARS))

    class Meta:
        model = get_user_model()
        fields = ('username', 'email', 'password1', 'password2', 'first_name', 'last_name', 'gender', 'birth_date', 'city', 'country')

    def signup(self, request, user):
        pass