【问题标题】:Django filtering on foreign key propertiesDjango过滤外键属性
【发布时间】:2017-02-04 20:41:56
【问题描述】:

我正在尝试根据外键的特定字段的值过滤 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

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

【问题讨论】:

  • 首先,你的代码不能产生这样的错误,因为你正在打印retailer.name并且在错误NoneType对象没有属性'location'

标签: python django


【解决方案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

【讨论】:

  • 如果将两个查询合并为一个,为什么不在一个查询中也使用select_related() 选择assigned_retailer,因为如果您不这样做,您将每@ 一次访问数据库987654325@
  • 没错,竖起大拇指
【解决方案2】:

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

但是,只有当零售商不是 None 时,您才能安全地导航每个 retailer 的属性,方法是使用 if 进行测试:

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

【讨论】:

    猜你喜欢
    • 2010-12-31
    • 2021-09-04
    • 2022-09-26
    • 2014-09-15
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    相关资源
    最近更新 更多