Python Django/Views执行两个表单
我真的有点麻烦了。我有一些自定义用户的设置,这些用户可以通过外键连接到公司。我只是救不了他们。我尝试了很多不同的方法让用户加入一家公司,但我就是无法破解。这些表单确实有效,它确实创建了一个“客户”和一个“客户公司” 我知道这需要是一种变化:Python Django/Views执行两个表单,python,django,Python,Django,我真的有点麻烦了。我有一些自定义用户的设置,这些用户可以通过外键连接到公司。我只是救不了他们。我尝试了很多不同的方法让用户加入一家公司,但我就是无法破解。这些表单确实有效,它确实创建了一个“客户”和一个“客户公司” 我知道这需要是一种变化: if customer_form.is_valid() and customer_company_form.is_valid(): customer_company = customer_company_form.save() custome
if customer_form.is_valid() and customer_company_form.is_valid():
customer_company = customer_company_form.save()
customer = customer_form.save(commit=False)
customer.user = customer_company
customer_company.save()
models.py
class CustomerCompany(models.Model):
COUNTRIES = (
('USA', 'United States'),
('CAN', 'Canada')
)
name = models.CharField(max_length=100, blank=True, unique=True)
website = models.CharField(max_length=100, blank=True)
phone = models.CharField(max_length=10, blank=True)
address = models.CharField(max_length=100, blank=True)
city = models.CharField(max_length=255, blank=True)
state = USStateField(blank=True, null=True)
us_zipcode = USZipCodeField(blank=True, null=True)
ca_province = models.CharField(max_length=50, blank=True, null=True)
ca_postal_code = models.CharField(max_length=7, blank=True, null=True)
country =models.CharField(max_length=3, choices=COUNTRIES,
blank=True)
def get_absolute_url(self):
return reverse('accounts:customer_company_detail',kwargs={'pk':self.pk})
def __str__(self):
return self.name
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,
primary_key=True, related_name='customer_profile')
company = models.ForeignKey(CustomerCompany, on_delete=models.CASCADE, null=True)
phone = models.CharField(max_length=10)
def __str__(self):
return self.user.first_name + ' ' + self.user.last_name
forms.py
class CustomerSignupForm(UserCreationForm):
first_name = forms.CharField(max_length=50, required=True)
last_name = forms.CharField(max_length=50, required=True)
phone = forms.CharField(max_length=10, required=True)
email = forms.EmailField(required=True)
class Meta(UserCreationForm.Meta):
model = User
@transaction.atomic
def save(self, commit=True):
user = super(CustomerSignupForm, self).save(commit=False)
user.is_customer = True
user.first_name = self.cleaned_data.get('first_name')
user.last_name = self.cleaned_data.get('last_name')
user.email = self.cleaned_data.get('email')
user.save()
customer = Customer.objects.create(user=user)
customer.phone = self.cleaned_data.get('phone')
customer.save()
return user
class CustomerCompanyCreateForm(forms.ModelForm):
ca_province = CAProvinceField(required=False, label="Province")
ca_postal_code = CAPostalCodeField(required=False, label="Postal Code")
class Meta:
model = CustomerCompany
fields = ['name', 'website', 'phone', 'address', 'city', 'state',
'us_zipcode', 'country', 'ca_province', 'ca_postal_code']
labels = {
"us_zipcode": "Zipcode",
}
views.py使用工作代码更新
def customer_signup(request):
if request.method == 'POST':
customer_form = CustomerSignupForm(request.POST)
customer_company_form = CustomerCompanyCreateForm(request.POST)
if customer_form.is_valid() and customer_company_form.is_valid():
# first save the user object
user_obj = customer_form.save(commit=False)
# Then use this object to get to my Customer model via the related name
customer = user_obj.customer_profile
# now save the CustomerCompany
company = customer_company_form.save()
# attach the customer to the Company
customer.company = company
# now fully save the customer after he's attached to his company
customer.save()
return redirect('customer_dashboard:customer_dashboard')
else:
messages.error(request, 'Please correct the errors below.')
else:
customer_form = CustomerSignupForm()
customer_company_form = CustomerCompanyCreateForm()
return render(request, 'accounts/registration/customer_signup_combined.html', {
'customer_form' : customer_form,
'customer_company_form' : customer_company_form,
})
您正在视图中保存这两个窗体,但没有连接这两个对象。 在
customer\u表单上调用save将返回User
对象,因为它是User
ModelForm
。您可以使用此对象通过Customer\u profile
related\u name
访问Customer
对象,并将company
字段设置为保存Customer\u company\u表单时返回的company
实例
应该是这样的:
if customer_form.is_valid() and customer_company_form.is_valid():
user_obj = customer_form.save(commit=True)
customer = user_obj.customer_profile
company = customer_company_form.save(commit=True)
customer.company = company
customer.save()
return redirect('customer_dashboard:customer_dashboard')
您正在视图中保存这两个窗体,但没有连接这两个对象。
在customer\u表单上调用save将返回User
对象,因为它是User
ModelForm
。您可以使用此对象通过Customer\u profile
related\u name
访问Customer
对象,并将company
字段设置为保存Customer\u company\u表单时返回的company
实例
应该是这样的:
if customer_form.is_valid() and customer_company_form.is_valid():
user_obj = customer_form.save(commit=True)
customer = user_obj.customer_profile
company = customer_company_form.save(commit=True)
customer.company = company
customer.save()
return redirect('customer_dashboard:customer_dashboard')
非常感谢。这是一个小的调整工作。我不得不删除客户\公司\表单上的commit=False,现在它工作得很好。我已经更新了上面的代码。谢谢!这是一个小的调整工作。我不得不删除客户\公司\表单上的commit=False,现在它工作得很好。我已经更新了上面的代码。