Tôi có một phương pháp vào mẫu đăng ký người dùng của tôi trông như thế này:Django thác tiết kiệm?
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
Vấn đề là khi tôi gọi form.save()
nó tạo ra đối tượng user
như mong đợi, nhưng không lưu hồ sơ hoặc địa chỉ của mình. Tại sao nó không xếp và lưu tất cả các mô hình phụ? Tôi nghi ngờ tôi có thể gọi user.profile.save()
và user.profile.address.save()
theo cách thủ công, nhưng tôi muốn toàn bộ điều để thành công hoặc thất bại cùng nhau. Cách tốt nhất để làm điều này là gì?
giải pháp hiện tại:
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()
tôi bị buộc phải profile
các "trung tâm" đối tượng. Cần thiết để thiết lập profile.user = user
thay vì user.profile = profile
để làm cho nó hoạt động (tôi đoán bởi vì khóa là trên mô hình hồ sơ, không phải trên mô hình người dùng). giải pháp
Mới hơn:
Tôi mất một gợi ý từ this article đề xuất trong this answer.
Bây giờ tôi đã tách ra hình thức mô hình của tôi và chuyển logic vào xem:
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)
)
Tôi không quá ngây thơ của chuyển gánh nặng để xem, nhưng tôi đoán tôi có được một cách linh hoạt hơn chút này đường?
Chương trình vui vẻ tuyệt vời! – jathanism