django-allauth:定制用户在/accounts/signup/处生成IntegrityError(定制字段为空或丢失) [英] django-allauth: custom user generates IntegrityError at /accounts/signup/ (custom fields are nulled or lost)

查看:152
本文介绍了django-allauth:定制用户在/accounts/signup/处生成IntegrityError(定制字段为空或丢失)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将django-allauth与自定义用户模型(子类AbstractUser)进行集成,但是当我测试注册表单时,由于字段(date_of_birth)为空,所以我收到一个完整性错误,但提交的值为u'1976 -4-6'

I'm trying to integrate django-allauth with a custom user model (subclassed AbstractUser, but when I test the signup form I get an integrity error due to field (date_of_birth) being null, but the value submitted was u'1976-4-6'

在学习django-allauth时,我正在学习新的自定义用户内容以及基于类的视图,因此我确信自己做错了事,但是在阅读了两天后github问题,一些教程,readthedocs和stackoverflow问题,我仍然不清楚我做错了什么(好吧,我知道我做错了一件事情:在这里和那里尝试不同的解决方案,所以我肯定会怀念-mosh的实现)

I'm learning the new custom user stuff, as well as class-based views as I'm learning django-allauth, so I'm confident that I'm doing something wrong, but after a couple days of reading the github issues, the few tutorials, readthedocs, and stackoverflow questions I still have no clear idea of what I'm doing wrong (well I know one thing I'm doing wrong: trying different solutions here and there, so I definitely have a miss-mosh of implementations)

但是,对于如何将allauth与子类AbstractUser集成在一起,我找不到很好的答案,因此,如果有人能启发我,我将非常感激.

But, I can't find a good answer on how to integrate allauth with a subclassed AbstractUser, so if anyone could enlighten me, I would really appreciate it.

(注意-当我以通过夹具加载的用户身份登录时,该站点或多或少都在工作,因此请假定非django-allauth遗漏是遗漏-如果您需要对以下内容进行说明,我会很高兴地编辑)

(Note - the site is more or less working when I log in as a user that I've loaded via fixtures, so please assume that non-django-allauth omissions are omissions - if you need clarification on something not below, I will happily edit)

AUTH_USER_MODEL = 'userdata.CtrackUser'
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_SIGNUP_FORM_CLASS = 'userdata.forms.SignupForm'
LOGIN_REDIRECT_URL = '/profile'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_AUTO_SIGNUP = False
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username'

userdata/models.py

class CtrackUser(AbstractUser):
    date_of_birth = models.DateField(help_text='YYYY-MM-DD format')
    gender = models.CharField(max_length=2, 
    choices=settings.GENDER_CHOICES, blank=True)
    race = models.CharField(max_length=2, choices=settings.RACE_CHOICES, null=True, blank=True)
    condition = models.ForeignKey(Condition, null=True, blank=True)
    location = models.CharField(max_length=255, null=True, blank=True)
    my_symptoms = models.ManyToManyField(Symptom)
    is_admin = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

userdata/forms.py

from django import forms
from django.conf import settings
from django.contrib.auth import get_user_model
from allauth.account.forms import SetPasswordField, PasswordField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from medical.models import Condition


class SignupForm(forms.Form):

    email = forms.EmailField(required=True,)
    username = forms.CharField(max_length=80,required=True,)
    password1 = SetPasswordField()
    password2 = PasswordField()
    first_name = forms.CharField(max_length=100,required=False,)
    last_name = forms.CharField(max_length=100, required=False,)
    date_of_birth = forms.DateField()   
    gender = forms.TypedChoiceField(
        choices=settings.GENDER_CHOICES,
        widget=forms.Select(attrs={'class': 'input-lg'}),
        required=False,)
    race = forms.TypedChoiceField(
        choices=settings.RACE_CHOICES,
        widget=forms.Select(attrs={'class': 'input-lg'}),
        required=False,)
    location = forms.CharField(max_length=255,required=False,)
    condition = forms.ModelChoiceField(
        queryset=Condition.objects.all(),
        widget=forms.Select(attrs={'class': 'input-lg'}),
        empty_label='Select condition (optional)'
    )

    class Meta:
        model = get_user_model() # use this function for swapping user model
        fields = ('email', 'username', 'password1',  'password2', 'first_name', 'last_name',
                  'date_of_birth', 'gender', 'race', 'location', 'condition', 'confirmation_key',)

    def __init__(self, *args, **kwargs):
        super(SignupForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'signup_form'
        self.helper.label_class = 'col-xs-6'
        self.helper.field_class = 'col-xs-12'
        self.helper.form_method = 'post'
        self.helper.form_action = 'accounts_signup'
        self.helper.add_input(Submit('submit', 'Sign up'))

    def signup(self, request, user, model):
        user.username = self.cleaned_data['username']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        model.date_of_birth = self.cleaned_data['date_of_birth']
        model.gender = self.cleaned_data['gender']
        model.race = self.cleaned_data['race']
        model.location = self.cleaned_data['location']
        model.condition = self.cleaned_data['condition']
        model.save()
        user.save()

templates/allauth/account/signup.html

<form id="signup_form" method="post" action="{% url 'account_signup' %}" class="form-inline">
  {% csrf_token %}
  {% crispy form %}
  {% if redirect_field_value %}
  <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
  {% endif %}
{#  <div class="form-actions">#}
{#  <button class="btn btn-primary" type="submit">Sign Up</button>#}
{#  </div>#}
</form>

POST数据

u'condition'  [u'1']
u'confirmation_key' [u'']
u'date_of_birth' [u'1976-4-6']
u'email' [u'1@bt.co']
u'first_name' [u'One']
u'gender' [u'']
u'last_name' [u'Person']
u'location' [u''] u'password1' [u'123456'] 
u'password2' [u'123456']
u'race' [u'']
u'submit' [u'Sign up']
u'username' [u'gn']

生成错误(注意与发布数据不同)

异常类型:/accounts/signup/

Error generated (note difference from post data)

Exception Type: IntegrityError at /accounts/signup/

异常值:出生日期"列中的空值违反了非空约束

Exception Value: null value in column "date_of_birth" violates not-null constraint

详细信息:失败行包含(19,pbkdf2_sha256 $ 12000 $ exNVzh4QI0Rb $ mCTz9Tc + TIBbD8 + lIZs2B3hqjxd + qmI ...,2014-07-02 16:27:43.751428 + 00,f,gn,One,Person,1 @ bt.co,f,t,2014-07-02 16:27:43.751473 + 00,null,,null,null,null,null,f,2014-07-02 16:27:43.833267 + 00,2014-07-02 16:27:43.83329 + 00).

DETAIL: Failing row contains (19, pbkdf2_sha256$12000$exNVzh4QI0Rb$mCTz9Tc+TIBbD8+lIZs2B3hqjxd+qmI..., 2014-07-02 16:27:43.751428+00, f, gn, One, Person, 1@bt.co, f, t, 2014-07-02 16:27:43.751473+00, null, , null, null, null, f, 2014-07-02 16:27:43.833267+00, 2014-07-02 16:27:43.83329+00).

此处的完整回溯: https://gist.githubusercontent.com/hanleybrand/ee260b55dfbb4f76b43d7b3dfb4dfb4dfb4dfb5d/gistfile1.txt

推荐答案

答案-我仍在弄清楚-似乎是,如果保存的模型中包含allauth.account.adapter.DefaultAccountAdapter不包含的字段类型,为了正确处理(例如,任何缺少__getitem__属性的字段,例如models.DateField),有必要实现类似于以下内容的自定义适配器.

The answer -- which I'm still figuring out -- seems to be that if you are saving a model that contains field types that allauth.account.adapter.DefaultAccountAdapter doesn't handle correctly (e.g. any field that lacks a __getitem__ attribute, like models.DateField) it is necessary to implement a custom adapter somewhat like below.

注释:您的子类化抽象用户模型是传入的用户,因此最佳实践是直接使用表单数据,例如 user.email = data.get('email') 比使用DefaultAccountAdapter类中使用的allauth内部方法

class AccountAdapter(DefaultAccountAdapter):
    def save_user(self, request, user, form, commit=False):
        data = form.cleaned_data
        user.email = data.get('email')
        user.username = data.get('username')
        # all your custom fields
        user.date_of_birth = data.get('date_of_birth')
        user.gender = data.get('gender')
        if 'password1' in data:
            user.set_password(data["password1"])
        else:
            user.set_unusable_password()
        self.populate_username(request, user)
        if commit:
            user.save()
        return user

这篇关于django-allauth:定制用户在/accounts/signup/处生成IntegrityError(定制字段为空或丢失)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆