Django过滤外键属性

时间:2016-09-27 09:22:31

标签: python django

我正在尝试根据外键的特定字段的值过滤Django中的表。

我的模特是:

class Retailer(SCOPEModel):
        """ A business or person that exchanges goods for vouchers with recipients
        """
        office = models.ForeignKey(Office, related_name='retailers')
        uuid = UUIDField(auto=True, version=4, null=True, help_text=_('unique id'))
        name = models.CharField(_('Name'), max_length=50, validators=[validate_sluggable],
                                                        help_text=_('Name of the retail shop'), blank=False)
        location = models.ForeignKey(Location, verbose_name=_('location'), blank=True, null=True,
                                                                 help_text=_('Location of the retail shop'), related_name='retailers')


class PointOfSaleTerminalAssignment(SCOPEModel):
        """Point Of Sale (POS) is the location where a transaction occurs for
        exchange of goods and services
        and a POS terminal is the hardware used to perform the transactions.
        These terminals are registered in the system.
        POS' are managed at office level
        """

        office = models.ForeignKey(Office, related_name='pos_terminals')
        terminal_type = models.ForeignKey(
                TerminalType,
                verbose_name=_('Terminal'),
                help_text=_("Device | Make (model)"),
        )
        wfp_number = models.CharField(
                _('WFP Number'),
                max_length=50,
                unique=True,
                validators=[validate_sluggable],
                help_text=_("WFP unique generated number e.g. Inventory number")
        )
        serial_number = models.CharField(
                _('Serial Number'),
                max_length=50,
                unique=True,
                help_text=_('Hardware device serial number')
        )
        slug = models.SlugField(
                editable=False,
                unique=True,
                help_text=_('Unique ID generated from the WFP number')
        )
        assigned_retailer = models.ForeignKey(
                Retailer,
                related_name='pos_terminals',
                null=True,
                blank=True,
                help_text=_('Retailer this POS terminal is assigned to')
        )

我想了解零售商及其分配的pos序列号的详细信息 目前我正在执行两个查询:

from maidea.apps.office.models import Office
from maidea.apps.redemption.models import Retailer, PointOfSaleTerminalAssignment

office = Office.objects.get(slug='so-co')
pos = PointOfSaleTerminalAssignment.objects.filter(office=office)

for p in pos:
    retailer = p.assigned_retailer
    print retailer.name

但是我得到了这个错误,请问我做错了什么? enter image description here

2 个答案:

答案 0 :(得分:1)

显然,并非所有PointOfSaleTerminalAssignment个实例都有assigned_retailer,因为 FK 字段可以采用 NULL 值。

但是,只有在零售商不是retailer的情况下,通过None进行测试,您才可以安全地导航每个if的属性:

for p in pos:
    retailer = p.assigned_retailer
    if retailer:
        print retailer.name

答案 1 :(得分:1)

您的assigned_retailer可以为空并且为空。 所以首先要检查是否有作业。

from maidea.apps.office.models import Office
from maidea.apps.redemption.models import Retailer, PointOfSaleTerminalAssignment

pos = PointOfSaleTerminalAssignment.objects.filter(office__slug='so-co')

for p in pos:
    if p.assigned_retailer:
        print p.assigned_retailer.name
相关问题