我有两个模型:具有外键carrierID的运输模型,并且它引用另一个模型Carrier。当我想添加新的运送时,出现上述错误。我所知道的,我没有carrierID_id字段,但我不知道为什么会收到此错误。
这是我的模型的结构:
class Carrier(models.Model):
class Meta:
verbose_name_plural='carriers'
carrierID=models.CharField(max_length=15, unique=True)
carrierName=models.CharField(max_length=50)
carrierTelephoneNumber=models.CharField(max_length=14)
carrierAddress=models.TextField()
def __str__(self):
return '{}{}{}{}'.format(self.carrierID,' ' , self.carrierName,' ', self.carrierTelephoneNumber,' ',self.carrierAddress)
class Shipping(models.Model):
shippingID = models.AutoField(primary_key=True)
carrierID = models.ForeignKey(Carrier, on_delete=models.CASCADE)
shippingDate = models.DateTimeField()
productName=models.CharField(max_length=30)
shippingAddress = models.TextField()
sendTo = models.CharField(max_length=50)
shippingStatus = models.BooleanField(default=False)
promisedDate=models.DateTimeField()
comment = models.TextField()
shippingForm的结构如下:
class ShippingForm(forms.ModelForm):
carrier = Carrier.objects.only('carrierID')
print(carrier)
carrierID = forms.ModelChoiceField(carrier)
shippingDate = forms.DateTimeField(required=False)
productName = forms.CharField(max_length=30, required=False)
shippingAddress = forms.Textarea()
sendTo = forms.CharField(max_length=50)
shippingStatus = forms.BooleanField(required=False)
promisedDate = forms.DateTimeField(required=False)
comment = forms.Textarea()
class Meta:
model=Shipping
fields=('carrierID','shippingDate', 'productName','shippingAddress', 'sendTo','shippingStatus','promisedDate', 'comment',)
这是发货的SQL。
create table pages_shipping(shippingID int auto_increment,
carrierID int(11),
shippingDate date,
prductName varchar(50),
shippingAddress text,
sendTo varchar(50),
shippingStatus boolean,
promisedDate date,
comment text,
primary key(shippingID),
foreign key(carrierID) REFERENCES pages_Carrier(id) ON DELETE CASCADE);
请协助
答案 0 :(得分:0)
我知道发生了什么事。这是因为当我运行py manage.py makemigrations
命令时,未添加字段carrierID。不幸的是,我不知道要强迫Django添加