【发布时间】:2015-04-25 10:31:29
【问题描述】:
这是我的模型:
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 字段。它根据Consignment 模型的price 和ProductPackage 模型的ammount_in_package 计算package_price。但是当makemigrations ForeignKey' object has no attribute 'volume' 时这段代码会抛出错误
package_price 是否会显示在 admin 页面中?我不需要它,因为它是自动计算的,不必允许管理员更改它。
【问题讨论】:
标签: python django python-2.7 django-models