Django filtering on foreign key properties
<p>I'm trying to filter a table in Django based on the value of a particular field of a foreign key.</p>

<p>my models are:</p>

<pre><code>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')
        )
</code></pre>

<p>i want to get details of retailers and their assigned pos serial numbers
Currently I am performing two queries:</p>

<pre><code>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
</code></pre>

<p>but am getting this error, kindly what am i doing wrong?
<a href="http://i.stack.imgur.com/srrkd.png" rel="nofollow"><img src="http://i.stack.imgur.com/srrkd.png" alt="enter image description here"></a></p>

<p>Apparently, not all your <code>PointOfSaleTerminalAssignment</code> instances has an <code>assigned_retailer</code>, as the <em>FK</em> field can take <em>NULL</em> values. </p>

<p>You can however <em>safely navigate</em> the attributes of each <code>retailer</code> only if the retailer is not <code>None</code> by testing with an <code>if</code>:</p>

<pre><code>for p in pos:
    retailer = p.assigned_retailer
    if retailer:
        print retailer.name
</code></pre>

python django