这是我的表单,当我保存它时,它只保存到用户模型而不保存在数据库中的客户端模型,我必须遗漏一些简单的但我无法弄明白。以下是表单和客户端模型
class RegisterForm(UserCreationForm):
email = forms.EmailField(label="Email")
fullname = forms.CharField(label="Full name")
type_choice= ( ('Customer','Customer'),('Supplier','Supplier'), )
type=forms.ChoiceField(choices=type_choice)
gender_choice=( ('Male','Male'), ('Female','Female'), )
gender=forms.ChoiceField(choices=gender_choice)
address=forms.CharField(label="Address",initial="Nothing")
phone_number=forms.IntegerField()
class Meta:
model= User
fields = ("username","fullname","email","type","gender","address","phone_number")
def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False)
first_name, last_name = self.cleaned_data["fullname"].split()
user.first_name = first_name
user.last_name = last_name
user.email = self.cleaned_data["email"]
user.type=self.cleaned_data["type"]
user.gender=self.cleaned_data["gender"]
user.address=self.cleaned_data["address"]
user.phone_number=self.cleaned_data["phone_number"]
client= Client(Client_ID=3,Client_FirstName=first_name,Client_PhoneNumber=user.phone_number)
if commit:
user.save()
client.save()
return user,client
这是客户端模型
class Client(models.Model):
Client_ID= models.IntegerField(primary_key=True)
Client_FirstName=models.CharField(max_length=30)
Client_LastName=models.CharField(max_length=30)
Client_Gender=models.CharField(max_length=30)
Client_PhoneNumber=models.IntegerField()
Client_Address=models.CharField(max_length=100)
Client_CreditRating=models.FloatField()
编辑:我正在关注此example以在表格中插入新条目