Cym
Cym

Reputation: 1

Django select_related chained foreign keys doesn't return non-direct related objects

I have been through multiple other posts so this one might seem a duplicate but I couldn't find an answer. I'm new on the platform so I apologies if this is not the proper way to proceed.

I use Django 2.2

Based on the official doc, it says that select_related() returns a QuerySet that will “follow” foreign-key relationships, selecting additional related-object data when it executes its query: https://docs.djangoproject.com/en/2.2/ref/models/querysets/#select-related

Here is the official example: You can follow foreign keys in a similar way to querying them. If you have the following models:

from django.db import models

class City(models.Model):
    # ...
    pass

class Person(models.Model):
    # ...
    hometown = models.ForeignKey(
        City,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
    )

class Book(models.Model):
    # ...
    author = models.ForeignKey(Person, on_delete=models.CASCADE)

then a call to Book.objects.select_related('author__hometown').get(id=4) will cache the related Person and the related City:

# Hits the database with joins to the author and hometown tables.
b = Book.objects.select_related('author__hometown').get(id=4)
p = b.author         # Doesn't hit the database.
c = p.hometown       # Doesn't hit the database.

# Without select_related()...
b = Book.objects.get(id=4)  # Hits the database.
p = b.author         # Hits the database.
c = p.hometown       # Hits the database.

Here is my code:

class Campaign(SlugifyNameMixin, TimeStampedModel, TimeFramedModel, 

    StatusModel):

        STATUS = Choices(
            ('draft', _('Draft')),
            ('published', _('Published')),
            ('disabled', _('Disabled')),
        )

        banner = ThumbnailerImageField(verbose_name=_('Banner'),
                                       help_text=_('Choose a representative banner image'),
                                       upload_to='campaigns/banners/',
                                       blank=True
                                      )
        tagline = models.CharField(max_length=150, verbose_name=_('Tagline'), help_text=_('A short campaign description'))


        organization = models.ForeignKey(Organization,
                                        on_delete=models.PROTECT,
                                        related_name='campaigns',
                                        verbose_name=_('Organization'),
                                        help_text=_('Select the organization that this campaign belongs to.')
                                        )

        description = RichTextUploadingField(null=False, default='',
                                             verbose_name=_('Description'),
                                             help_text=_('A description of this campaign and what it aims to accomplish')
                                             )

        class Meta:
            verbose_name = _("Campaign")
            verbose_name_plural = _("Campaigns")
            unique_together = ("organization", "slug")

            permissions = (('change_campaign_status', 'Can change the status of campaigns'),)

        def __str__(self):
            return '%s' % (self.name)

        def get_absolute_url(self):
            return reverse('explore:campaign-detail', args=[self.organization.slug, self.slug])


    class Action(SlugifyNameMixin, TimeStampedModel):

        campaign = models.ForeignKey(Campaign,
                                     on_delete=models.PROTECT,
                                     related_name='actions',
                                     verbose_name=_('campaign'),
                                     help_text=_('Select the campaign that this action belongs to.')
                                    )

        class Meta:
            verbose_name = _('Action')
            verbose_name_plural = _('Actions')

            permissions = (('change_action_status', 'Can change the status of actions'),)

        def __str__(self):
            return '%s' % (self.name)


    class ActionMeta(TimeStampedModel, StatusModel):

        STATUS = Choices(
            ('open', _('Open')),
            ('in-progress', _('In-Progress')),
            ('completed', _('Completed')),
        )

        user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), on_delete=models.PROTECT)
        action = models.ForeignKey(Action, on_delete=models.CASCADE, related_name='metas', verbose_name=_('action'))


        class Meta:
            verbose_name = _('Action Meta')
            verbose_name_plural = _('Action Metas')
            unique_together = ('user', 'action')

        def __str__(self):
            return '%s-meta-%s' % (self.action.name, self.id)

My problem is that I can't retrieve the campaign objects

>>> camp11 = Campaign.objects.get(name='camp11')
>>> camp12 = Campaign.objects.get(name='camp12')
>>> action11u1 = Action.objects.get(name='action11u1')
>>> action12u1 = Action.objects.get(name='action12u1')
>>> am1 = ActionMeta.objects.create(user=u, action=action11u1)
>>> am2 = ActionMeta.objects.create(user=u, action=action12u1)

>>> a1 = ActionMeta.objects.select_related('action__campaign').get(pk=am1.pk)
>>> a1.campaign
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'ActionMeta' object has no attribute 'campaign'

>>> a1.action
<Action: action11u1>

Here is the DB query

>>> print(a1.query)
SELECT "core_actionmeta"."id", "core_actionmeta"."created", "core_actionmeta"."modified", "core_actionmeta"."status", "core_actionmeta"."status_changed", "core_actionmeta"."user_id", "core_actionmeta"."action_id", "core_action"."id", "core_action"."created", "core_action"."modified", "core_action"."name", "core_action"."slug", "core_action"."campaign_id", "core_campaign"."id", "core_campaign"."created", "core_campaign"."modified", "core_campaign"."start", "core_campaign"."end", "core_campaign"."status", "core_campaign"."status_changed", "core_campaign"."name", "core_campaign"."name_en", "core_campaign"."name_fa", "core_campaign"."slug", "core_campaign"."banner", "core_campaign"."tagline", "core_campaign"."tagline_en", "core_campaign"."tagline_fa", "core_campaign"."organization_id", "core_campaign"."description", "core_campaign"."description_en", "core_campaign"."description_fa" FROM "core_actionmeta" INNER JOIN "core_action" ON ("core_actionmeta"."action_id" = "core_action"."id") INNER JOIN "core_campaign" ON ("core_action"."campaign_id" = "core_campaign"."id") WHERE "core_actionmeta"."id" = 3

Upvotes: 0

Views: 694

Answers (1)

RishiG
RishiG

Reputation: 2830

You still need to go through action. Try:

>>> a1.action.campaign

Side note: I'd probably change the name of ActionMeta just because Meta has such a specific meaning, and there already is an Action.Meta.

Upvotes: 1

Related Questions