简体   繁体   中英

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.

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:

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.

You can however safely navigate the attributes of each retailer only if the retailer is not None by testing with an if :

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

Your assigned_retailer can be blank and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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