這裏是我的模型:計算在Django模型
class Consignment(models.Model):
number = models.IntegerField(unique=True)
creation_date = models.DateTimeField()
expiration_date = models.DateTimeField()
package_ammount = models.IntegerField()
price = models.DecimalField(max_digits=12, decimal_places=2)
volume = models.DecimalField(max_digits=8, decimal_places=3)
image = models.ImageField()
brand = models.ForeignKey(Brand)
def __unicode__(self):
return self.brand.name + ' ' + str(self.volume) + ' liters'
class ProductPackage(models.Model):
consignment = models.ForeignKey(Consignment)
ammount_in_package = models.IntegerField()
total_volume = consignment.volume*ammount_in_package
total_width = models.DecimalField(max_digits=6, decimal_places=3)
total_height = models.DecimalField(max_digits=6, decimal_places=3)
total_length = models.DecimalField(max_digits=6, decimal_places=3)
package_price = consignment.price*ammount_in_package
問題是與package_price
領域。它計算package_price
是基於price
的Consignment
模型和ammount_in_package
的ProductPackage
模型。但是這段代碼會拋出並且錯誤時makemigrations
ForeignKey' object has no attribute 'volume'
而且package_price
會在admin
頁面顯示嗎?我不需要它,因爲它會自動計算,因此不必允許管理員更改它。