Django allauth custom signup form error: 'Institution' object is not iterable
Django allauth custom signup form error: 'Institution' object is not iterable
我可能在这里遗漏了一些明显的东西,但我收到以下错误:
'Institution' object is not iterable
不喜欢代码中的这一行:
user.institution = institution
这是 Django allauth 的完整注册表格 class:
class SignupForm(forms.Form):
first_name = forms.CharField(
max_length=255,
label="Your First Name",
)
last_name = forms.CharField(
max_length=255,
label="Your Last Name",
)
institution = forms.ModelChoiceField(
label="Your Institution",
queryset=Institution.objects.all(),
empty_label="Other Institution Not Listed",
)
def signup(self, request, user):
from pprint import pprint
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.institution = self.cleaned_data['institution']
user.save()
我检查过self.cleaned_data
,它似乎正确地通过了机构:
{'email': 'blah@blah',
'first_name': 'John',
'institution': <Institution: Blah College>,
'last_name': 'Doe'}
我使用的是自定义用户模型,机构是多对多的:
institution = models.ManyToManyField(Institution, db_index=True)
有什么想法吗?我在这里做错了什么?
如果是多对多关系,则需要使用 add
方法将机构与用户相关联:
user.institution.add(self.cleaned_data['institution'])
您可以阅读有关多对多关系操作的更多信息here。
我可能在这里遗漏了一些明显的东西,但我收到以下错误:
'Institution' object is not iterable
不喜欢代码中的这一行:
user.institution = institution
这是 Django allauth 的完整注册表格 class:
class SignupForm(forms.Form):
first_name = forms.CharField(
max_length=255,
label="Your First Name",
)
last_name = forms.CharField(
max_length=255,
label="Your Last Name",
)
institution = forms.ModelChoiceField(
label="Your Institution",
queryset=Institution.objects.all(),
empty_label="Other Institution Not Listed",
)
def signup(self, request, user):
from pprint import pprint
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.institution = self.cleaned_data['institution']
user.save()
我检查过self.cleaned_data
,它似乎正确地通过了机构:
{'email': 'blah@blah',
'first_name': 'John',
'institution': <Institution: Blah College>,
'last_name': 'Doe'}
我使用的是自定义用户模型,机构是多对多的:
institution = models.ManyToManyField(Institution, db_index=True)
有什么想法吗?我在这里做错了什么?
如果是多对多关系,则需要使用 add
方法将机构与用户相关联:
user.institution.add(self.cleaned_data['institution'])
您可以阅读有关多对多关系操作的更多信息here。