简体   繁体   English

Django过滤外键属性

[英]Django filtering on foreign key properties

I'm trying to filter a table in Django based on the value of a particular field of a foreign key. 我试图根据外键的特定字段的值来过滤Django中的表。

my models are: 我的模型是:

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')
        )

i want to get details of retailers and their assigned pos serial numbers Currently I am performing two queries: 我想获取零售商及其分配的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

but am getting this error, kindly what am i doing wrong? 但出现此错误,请问我在做什么错? 在此处输入图片说明

Apparently, not all your PointOfSaleTerminalAssignment instances has an assigned_retailer , as the FK field can take NULL values. 显然,并非所有PointOfSaleTerminalAssignment实例有assigned_retailer ,作为FK字段可以接受NULL值。

You can however safely navigate the attributes of each retailer only if the retailer is not None by testing with an if : 但是,仅当零售商不是None ,才可以通过if进行测试来安全地导航每个retailer的属性:

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

Your assigned_retailer can be blank and null. 您的assigned_retailer可以为空,可以为null。 So first of all check if there is assignment. 因此,首先检查是否有分配。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM