gpt4 book ai didi

django-allauth:自定义用户在/accounts/signup/生成 IntegrityError(自定义字段为空或丢失)

转载 作者:行者123 更新时间:2023-12-04 04:26:32 24 4
gpt4 key购买 nike

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

我正在学习新的自定义用户内容,以及基于类的 View ,因为我正在学习 django-allauth,所以我确信我做错了什么,但是在阅读了 github 问题几天后,少数教程、readthedocs 和 stackoverflow 问题我仍然不清楚我做错了什么(好吧我知道我做错了一件事:在这里和那里尝试不同的解决方案,所以我肯定有实现)

但是,我找不到关于如何将 allauth 与子类 AbstractUser 集成的好答案,所以如果有人能启发我,我将不胜感激。

(注意 - 当我以我通过装置加载的用户身份登录时,该站点或多或少地工作,所以请假设非 django-allauth 遗漏是遗漏 - 如果您需要澄清一些不在下面的内容,我很乐意编辑)

设置.py

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'

用户数据/模型.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)

用户数据/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()

模板/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>

发布数据
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/处的 IntegrityError

异常值:“date_of_birth”列中的空值违反了非空约束

详细信息:失败的行包含 (19, pbkdf2_sha256$12000$exNVzh4QI0Rb$mCTz9Tc+TIBbD8+lIZs2B3hqjxd+qmI..., 2014-07-02 16:27:43.75@One,fbgn+01 , 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/ee260b53dfb404f5055a/raw/3325dc746120c4f7521b9b976abce45dd7d71a77/gistfile1.txt

最佳答案

答案——我还在想——似乎是,如果你保存的模型包含 allauth.account.adapter.DefaultAccountAdapter 的字段类型无法正确处理(例如,任何缺少 __getitem__ 属性的字段,如 models.DateField ),有必要实现一个类似于下面的自定义适配器。

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

用户数据/适配器.py

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(自定义字段为空或丢失),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24536774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com