Opened 6 years ago

Closed 11 months ago

#14887 closed New feature (duplicate)

select_related() does not work with Proxy models and multi-table inheritance

Reported by: Ara Anjargolian <ara818@…> Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords: proxy select_related inheritance
Cc: flisky Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Consider the following models

class TumbleItem(models.Model):
    tumblr_id = models.CharField(max_length=64, editable=False, null=True)
    pub_date = models.DateTimeField(
    format = models.CharField(max_length=10, choices=FORMAT_CHOICES, default='html')
    display_status = models.CharField(max_length=10, choices=DISPLAY_STATUS_CHOICES, default='public')
    tags = TaggableManager()

    # this is for magic later and makes doing lookups both easier and lazier.
    content_type = models.ForeignKey(ContentType, editable=False, null=True)
    object_id = models.PositiveIntegerField(db_index=True, editable=False, null=True)
    object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_id")

class Regular(TumbleItem):
    title = models.CharField(max_length=250, blank=True)
    body = models.TextField()

class StoryItem(TumbleItem):
    class Meta:
        proxy = True
    def _slug(self):
        strip out extra punctuation and create a slug
        slug = ''
        if self.object.title != '':
            slug = base.get_base_term(self.object.title)
        return slug
    slug = property(_slug)

To access data stored in Regular efficiently, a select_related would be useful. While a select_related has the expected behavior when using TumbleItem, using the proxy model StoryItem it breaks. I would expect them to have the exact same behavior. See below

>>> print TumbleItem.objects.select_related('regular').query
SELECT `djumblr_tumbleitem`.`id`, `djumblr_tumbleitem`.`tumblr_id`, `djumblr_tumbleitem`.`pub_date`, `djumblr_tumbleitem`.`format`, `djumblr_tumbleitem`.`display_status`, `djumblr_tumbleitem`.`content_type_id`, `djumblr_tumbleitem`.`object_id`, `djumblr_regular`.`tumbleitem_ptr_id`, `djumblr_regular`.`title`, `djumblr_regular`.`body` FROM `djumblr_tumbleitem` LEFT OUTER JOIN `djumblr_regular` ON (`djumblr_tumbleitem`.`id` = `djumblr_regular`.`tumbleitem_ptr_id`) ORDER BY `djumblr_tumbleitem`.`pub_date` DESC

>>> print StoryItem.objects.select_related('regular').query
SELECT `djumblr_tumbleitem`.`id`, `djumblr_tumbleitem`.`tumblr_id`, `djumblr_tumbleitem`.`pub_date`, `djumblr_tumbleitem`.`format`, `djumblr_tumbleitem`.`display_status`, `djumblr_tumbleitem`.`content_type_id`, `djumblr_tumbleitem`.`object_id` FROM `djumblr_tumbleitem` ORDER BY `djumblr_tumbleitem`.`pub_date` DESC

Change History (7)

comment:1 Changed 6 years ago by Ara Anjargolian <ara818@…>

  • Component changed from Uncategorized to Database layer (models, ORM)
  • Keywords proxy select_related inheritance added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by jaddison

  • Severity set to Normal
  • Type set to New feature

This would be a nice-to-have addition to existing functionality - marking as 'new feature'.

comment:4 Changed 5 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:5 Changed 5 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:6 Changed 4 years ago by flisky

  • Cc flisky added

comment:7 Changed 11 months ago by timgraham

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #18012. The patch proposed on that ticket fixes this issue.

Note: See TracTickets for help on using tickets.
Back to Top