0

这是我的表单,当我保存它时,它只保存到数据库中的用户模型而不是客户端模型,我一定遗漏了一些简单的东西,但我无法弄清楚。以下是Form和Client模型

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()

编辑:我按照这个例子在表中插入一个新条目

4

0 回答 0