Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UserProfile缺少用户对象_Python_Django_Django Allauth - Fatal编程技术网

Python UserProfile缺少用户对象

Python UserProfile缺少用户对象,python,django,django-allauth,Python,Django,Django Allauth,我正在使用django allauth的自定义注册表单 设置.py ACCOUNT_SIGNUP_FORM_CLASS = 'project.userprofile.form.UserSignupForm' form.py from django import forms from models import UserProfile class UserSignupForm(forms.ModelForm): class Meta: model = UserProfi

我正在使用django allauth的自定义注册表单

设置.py

ACCOUNT_SIGNUP_FORM_CLASS = 'project.userprofile.form.UserSignupForm'
form.py

from django import forms
from models import UserProfile

class UserSignupForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('mobile_number',)
models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    mobile_number = models.CharField(max_length=30, blank=True, null=True)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
创建了
User
UserProfile
对象,但是UserProfile与任何用户对象都没有关联。很晚了,我可能错过了一些愚蠢的事情,对吧

更新:正如Kevin指出的,解决方案是在form.py中添加save方法。这就是它现在的样子:

报告说:

[
ACCOUNT\u SIGNUP\u FORM\u CLASS
]应该实现一个“save”方法,接受新注册的用户作为其唯一参数


看起来您还没有提供这样的方法,因此用户从未连接到配置文件。我认为您没有看到错误,因为
ModelForm
有一个
save(commit=True)
方法恰好与此签名匹配,即使它不符合您的要求。

谢谢Kevin!这对我很有效,我会更新我的问题。@henriquea:太好了,很乐意帮忙。唯一需要注意的是,您可能不应该尝试在其他上下文中使用
ModelForm
,因为您的
save()
方法现在正在隐藏
ModelForm.save()
。我刚刚向django allauth提交了一份申请,建议更改名字。凯文说得对!那我就另谋高就。干杯
from django import forms
from django.contrib.auth.models import User
from models import UserProfile

class UserSignupForm(forms.ModelForm):

    class Meta:
        model = UserProfile
        fields = ('mobile_number',)

    def save(self, user):
        profile = UserProfile(user=user)
        profile.mobile_number = self.cleaned_data['mobile_number']
        profile.save()