这里是我的模型:计算在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
页面显示吗?我不需要它,因为它会自动计算,因此不必允许管理员更改它。