emails_for_help字段中。
ManyToManyField创建实例时,不能向其添加对象。看一下doc。
class Set_user(FormView): template_name="pkm_templates/set_up_user.html" form_class = Set_User_Form success_url = '/thanks/' def form_valid(self, form): org = form.cleaned_data.get('organization') emails = form.cleaned_data.get("share_email_with") users = User.objects.filter(email__in=emails) instance = Setupuser.objects.create(organization=org) for user in users: instance.emails_for_help.add(user) return redirect("/")
编辑
这样做的另一种方法是使用
.set()。
class Set_user(FormView): template_name="pkm_templates/set_up_user.html" form_class = Set_User_Form success_url = '/thanks/' def form_valid(self, form): org = form.cleaned_data.get('organization') emails = form.cleaned_data.get("share_email_with") users = User.objects.filter(email__in=emails) instance = Setupuser.objects.create(organization=org) instance.emails_for_help.set(users) return redirect("/")
或者,你可以简单地使用
.add()添加任意数量的对象。
class Set_user(FormView): template_name="pkm_templates/set_up_user.html" form_class = Set_User_Form success_url = '/thanks/' def form_valid(self, form): org = form.cleaned_data.get('organization') emails = form.cleaned_data.get("share_email_with") users = User.objects.filter(email__in=emails) instance = Setupuser.objects.create(organization=org) instance.emails_for_help.add(*users) return redirect("/")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)