models.py
class Product(models.Model):
ncm = models.CharField(max_length=8)
product = models.CharField(_('Produto'), max_length=60, unique=True)
class SaleDetail(models.Model):
sale = models.ForeignKey(Sale, related_name='sales_det')
product = models.ForeignKey(Product, related_name='product_det')
quantity = models.PositiveSmallIntegerField(_('quantidade'))
def __unicode__(self):
return unicode(self.sale)
def get_ncm(self):
return self.'ncm'
模板
<td>{{ item.get_ncm }} </td>
我需要
<td>{{ product.ncm }} </td>
如何在SaleDetail模板中返回ncm的产品?
答案 0 :(得分:1)
来自产品:
{{ some_product.ncm }}
来自SaleDetail:
{{ some_sale_detail.product.ncm }}
来自产品:
class Product(models.Model):
def get_ncm(self):
return self.ncm
来自SaleDetail:
class SaleDetail(models.Model):
def get_ncm(self):
return self.product.ncm