我已经创建了一个模型并与之连接了外键,但是保存它们时出现错误无法分配“ 1”:“ SupplierBidModel.Load_ID”必须是“ ShipperBoardModel”实例。
我对Django相当陌生,甚至对Django互连模型概念也不陌生。知道为什么会这样吗?我也不知道该从哪里去,因为在Google上也没有很多类似的问题。
我的模型。py
class ShipperBoardModel(models.Model):
# Manufaturer_ID
LoadID = models.AutoField(primary_key=True)
From = models.CharField(max_length=100,null=True)
To = models.CharField(max_length=100,null=True)
Type = models.CharField(max_length=100,null=True)
Length = models.CharField(max_length=100,null=True)
Weight = models.CharField(max_length=100,null=True)
Numberoftrucks = models.IntegerField(null=True)
MaterialType = models.CharField(null=True,max_length=100)
Loadingtime = models.DateTimeField(null=True)
# Loadkey = models.ForeignKey()
def _str_(self):
return self.Origin
#
class SupplierBidModel(models.Model):
BidID = models.AutoField(primary_key=True)
Load_ID = models.ForeignKey(ShipperBoardModel,on_delete=models.CASCADE,default=3)
Supplier_ID = models.ForeignKey(SupplierBoardModel,on_delete=models.CASCADE,default=3)
Bid_amount = models.IntegerField(null=True)
class Meta:
unique_together = ('Load_ID', 'Supplier_ID')
Views.py
def suppliertablefun (request): # function to display shipperboardmodel
data = ShipperBoardModel.objects.all()
if request.method == 'POST':
forminput = BiddingForm(request.POST)
if forminput.is_valid():
forminput.save()
forminput = BiddingForm(request.POST)
return render(request, 'supplierboard/board.html', locals(), {'forminput': forminput})
PS:我正在尝试这样填写表单:
<form action="" method="post">
{# <input type="hidden" value={{item.LoadID}} name="Load_ID" /> #}
<input type="hidden" value={{item.LoadID}} name="Load_ID" />
{{ forminput.Bid_amount }}
<input type="submit" class="btn btn-primary" value="Submit"/>
</form>
我试图通过使用value = {{item.LoadID}}来传递当前LoadID到LoadID中,这是因为模板中存在for循环:
{% for item in data %}
Forms.py
class BiddingForm(forms.ModelForm):
Bid_amount = forms.IntegerField()
Load_ID = forms.IntegerField(widget=forms.HiddenInput())
class Meta:
model = SupplierBidModel
exclude = ()
好,所以我只是通过传递值= {{LoadID}}而不是value={{item.LoadID}}
来修复该错误,没有错误,但是它没有将任何内容保存到数据库中。现在该怎么办?
答案 0 :(得分:1)
好吧,您的模型有问题
class SupplierBidModel(models.Model):
BidID = models.AutoField(primary_key=True)
Load_ID = models.ForeignKey(ShipperBoardModel,on_delete=models.CASCADE,default=3,related_name='load')
Supplier_ID = models.ForeignKey(SupplierBoardModel,on_delete=models.CASCADE,default=3,related_name='supplier')
Bid_amount = models.IntegerField(null=True)
单个模型中不能有多个。为此,您需要related_name参数
答案 1 :(得分:1)
您试图在代码中的某个地方为SupplierBidModel.Load_ID
分配一个数字(1),这是不正确的,因为您的SupplierBidModel
中包含以下内容:
Load_ID = models.ForeignKey(ShipperBoardModel,on_delete=models.CASCADE,default=3,related_name='load')
如果您使用数字链接ShipperBidModel
来创建ShipperBoardModel
,则需要首先获取相应的对象,类似于:
# ... you get load_id from the form
shipper_board = ShipperBoardModel.objects.get(pk=load_id)
# then you create the SupplierBidModel instance, adding the other fields that you want
SupplierBidModel.objects.create(LoadId=shipper_board)
您的LoadId
不是id,而是ShipperBoardModel
对象,因此您需要分配相应的对象。
您的命名约定到处都是,模型字段应小写,并用下划线分隔。检查文档:Model style