gpt4 book ai didi

Django级联保存?

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

我的用户注册表单上有一个方法,如下所示:

def save(self):
user = User(
username = self.cleaned_data['username'],
email = self.cleaned_data['email1'],
first_name = self.cleaned_data['first_name'],
last_name = self.cleaned_data['last_name'],
)
user.set_password(self.cleaned_data['password1'])
user.profile = Profile(
primary_phone = self.cleaned_data['phone'],
)
user.profile.address = Address(
country = self.cleaned_data['country'],
province = self.cleaned_data['province'],
city = self.cleaned_data['city'],
postal_code = self.cleaned_data['postal_code'],
street1 = self.cleaned_data['street1'],
street2 = self.cleaned_data['street2'],
street3 = self.cleaned_data['street3'],
)
user.save()
return user

问题是当我调用 form.save()它创建 user按预期对象,但不保存他的个人资料或地址。为什么不级联保存所有子模型?我怀疑我可以调用 user.profile.save()user.profile.address.save()手动,但我希望整个事情一起成功或失败。最好的方法是什么?

当前解决方案:
def save(self):
address = Address(
country = self.cleaned_data['country'],
province = self.cleaned_data['province'],
city = self.cleaned_data['city'],
postal_code = self.cleaned_data['postal_code'],
street1 = self.cleaned_data['street1'],
street2 = self.cleaned_data['street2'],
street3 = self.cleaned_data['street3'],
)
address.save()

user = User(
username = self.cleaned_data['username'],
email = self.cleaned_data['email1'],
first_name = self.cleaned_data['first_name'],
last_name = self.cleaned_data['last_name'],
)
user.set_password(self.cleaned_data['password1'])
user.save()

profile = Profile(
primary_phone = self.cleaned_data['phone'],
)
profile.address = address
profile.user = user
profile.save()

我必须制作 profile “中心”对象。需要设置 profile.user = user而不是 user.profile = profile让它工作(我猜是因为关键在配置文件模型上,而不是在用户模型上)。

较新的解决方案:

我从 this article 那里得到了提示建议在 this answer .

现在我已经分离了我的模型表单并将逻辑移到了 View 中:
def register(request):
if request.POST:
account_type_form = forms.AccountTypeForm(request.POST)
user_form = forms.UserForm(request.POST)
profile_form = forms.ProfileForm(request.POST)
address_form = forms.AddressForm(request.POST)

if user_form.is_valid() and profile_form.is_valid() and address_form.is_valid():
user = user_form.save()
address = address_form.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.address = address
profile.save()
return HttpResponseRedirect('/thanks/')
else:
account_type_form = forms.AccountTypeForm()
user_form = forms.UserForm()
profile_form = forms.ProfileForm()
address_form = forms.AddressForm()

return render_to_response(
'register.html',
{'account_type_form': account_type_form, 'user_form': user_form, 'address_form': address_form, 'profile_form': profile_form},
context_instance=RequestContext(request)
)

我不太喜欢将负担转移到 View 上,但我想这样我会获得更多的灵 active ?

最佳答案

它不会级联保存,因为它实际上并不知道是否需要保存其他对象。

一次性完成,首先start a transaction :

@transaction.commit_on_success
def save(self):
....

然后按顺序保存子对象:
  user.profile.address.save()
user.profile.save()
user.save()

关于Django级联保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2256131/

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