如何为其他型号返回字段ncm?

时间:2014-12-24 05:11:58

标签: django django-models field

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的产品?

1 个答案:

答案 0 :(得分:1)

模板访问

来自产品:

{{ some_product.ncm }}

来自SaleDetail:

{{ some_sale_detail.product.ncm }}

Python访问

来自产品:

class Product(models.Model):
    def get_ncm(self):
        return self.ncm

来自SaleDetail:

class SaleDetail(models.Model):
    def get_ncm(self):
        return self.product.ncm