Opened 15 years ago
Closed 10 years ago
#14887 closed New feature (duplicate)
select_related() does not work with Proxy models and multi-table inheritance
| Reported by: | 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 |
Description
Consider the following models
class TumbleItem(models.Model):
tumblr_id = models.CharField(max_length=64, editable=False, null=True)
pub_date = models.DateTimeField(default=datetime.datetime.now)
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 by , 15 years ago
| Component: | Uncategorized → Database layer (models, ORM) |
|---|---|
| Keywords: | proxy select_related inheritance added |
comment:2 by , 15 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → New feature |
comment:6 by , 14 years ago
| Cc: | added |
|---|
comment:7 by , 10 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Duplicate of #18012. The patch proposed on that ticket fixes this issue.
Note:
See TracTickets
for help on using tickets.
This would be a nice-to-have addition to existing functionality - marking as 'new feature'.